Skip to content

Commit

Permalink
fix olm cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
williscool committed Aug 11, 2023
1 parent e851a28 commit 9a2c877
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 31 deletions.
41 changes: 16 additions & 25 deletions spec/workload/operator_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@ require "helm"
require "file_utils"
require "sam"
require "json"
require "../../utils/operator.cr"
require "../../src/tasks/utils/operator.cr"

def run_test_with_cleanup(cnf_path : String, cleanup : Proc)
begin
LOGGING.info `./cnf-testsuite -l info cnf_setup cnf-path=#{cnf_path}`
$?.success?.should be_true
ensure
LOGGING.info `./cnf-testsuite -l info cnf_cleanup cnf-path=#{cnf_path}`
$?.success?.should be_true
Operator::OLM.cleanup
cleanup.call
end
end

describe "Operator" do
describe "pre OLM install" do
it "'operator_test' operator should not be found", tags: ["operator_test"] do
begin
LOGGING.info `./cnf-testsuite cnf_setup cnf-path=sample-cnfs/sample_coredns`
$?.success?.should be_true
run_test_with_cleanup("sample-cnfs/sample_coredns", -> {}) do
resp = `./cnf-testsuite -l info operator_installed`
Log.info { "#{resp}" }
(/NA: No Operators Found/ =~ resp).should_not be_nil
ensure
LOGGING.info `./cnf-testsuite cnf_cleanup cnf-path=sample-cnfs/sample_coredns`
$?.success?.should be_true
end
end
end
Expand All @@ -48,41 +54,26 @@ describe "Operator" do
end

it "'operator_test' test if operator is being used", tags: ["operator_test"] do
begin
LOGGING.info `./cnf-testsuite -l info cnf_setup cnf-path=./sample-cnfs/sample_operator`
$?.success?.should be_true
run_test_with_cleanup("./sample-cnfs/sample_operator", -> {}) do
resp = `./cnf-testsuite -l info operator_installed`
Log.info { "#{resp}" }
(/PASSED: Operator is installed/ =~ resp).should_not be_nil
ensure
LOGGING.info `./cnf-testsuite -l info cnf_cleanup cnf-path=./sample-cnfs/sample_operator`
$?.success?.should be_true
end
end

it "'operator_privileged' test privileged operator NOT being used", tags: ["operator_privileged"] do
begin
LOGGING.info `./cnf-testsuite -l info cnf_setup cnf-path=./sample-cnfs/sample_operator`
$?.success?.should be_true
run_test_with_cleanup("./sample-cnfs/sample_operator", -> {}) do
resp = `./cnf-testsuite -l info operator_privileged`
Log.info { "#{resp}" }
(/PASSED: Operator is NOT running with privileged rights/ =~ resp).should_not be_nil
ensure
LOGGING.info `./cnf-testsuite -l info cnf_cleanup cnf-path=./sample-cnfs/sample_operator`
$?.success?.should be_true
end
end

it "'operator_privileged' test if a privileged operator is being used", tags: ["operator_privileged"] do
begin
LOGGING.info `./cnf-testsuite -l info cnf_setup cnf-path=./sample-cnfs/sample_privileged_operator`
$?.success?.should be_true
run_test_with_cleanup("./sample-cnfs/sample_privileged_operator", -> {}) do
resp = `./cnf-testsuite -l info operator_privileged`
Log.info { "#{resp}" }
(/FAILED: Operator is running with privileged rights/ =~ resp).should_not be_nil
ensure
LOGGING.info `./cnf-testsuite -l info cnf_cleanup cnf-path=./sample-cnfs/sample_privileged_operator`
$?.success?.should be_true
end
end
end
Expand Down
35 changes: 35 additions & 0 deletions src/tasks/operator_setup.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "sam"
require "file_utils"
require "colorize"
require "totem"
require "./utils/utils.cr"
require "tar"
require "./utils/operator.cr"

desc "Install Operator Lifecycle Manager"
task "install_olm" do |_, args|
install_status = Operator::OLM.install

if install_status
stdout_success "Operator Lifecycle Manager successfully installed"
else
stdout_failure "Operator Lifecycle Manager installation failed"
exit 1
end
end

desc "Uninstall and Cleanup Operator Lifecycle Manager"
task "cleanup_olm" do |_, args|
uninstall_status = Operator::OLM.cleanup

if uninstall_status
stdout_success "Operator Lifecycle Manager was uninstalled successfully"
else
stdout_failure "Operator Lifecycle Manager could not be uninstalled."
exit 1
end
end

desc "Uninstall and Cleanup Operator Lifecycle Manager"
task "uninstall_olm", ["cleanup_olm"] do |_, args|
end
38 changes: 33 additions & 5 deletions utils/operator.cr → src/tasks/utils/operator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require "log"
require "kubectl_client"
require "helm"
require "file_utils"
require "../src/tasks/utils/utils.cr"
require "./utils.cr"

module Operator
module OLM
Expand Down Expand Up @@ -56,10 +56,28 @@ module Operator
KubectlClient::Get.resource_wait_for_uninstall("Pod", "#{pod_name}", 180, "operator-lifecycle-manager")
end

self.clear_namespaces(["operators", "operator-lifecycle-manager", "simple-privileged-operator"])
namespace_clear_results = self.clear_namespaces(["operators", "operator-lifecycle-manager", "simple-privileged-operator"])

Log.info { "namespace_clear_results: #{namespace_clear_results}" }

# check if each namespace is removed
all_namespaces_cleared = true

namespace_clear_results.each do |namespace, result|
if result[:status].success?
Log.info { "Namespace #{namespace} removed successfully" }
else
Log.error { "Namespace #{namespace} failed to remove" }
all_namespaces_cleared = false
end
end

all_namespaces_cleared
end

def self.clear_namespaces(namespaces)
results = {} of String => {status: Process::Status, output: String, error: String}

namespaces.each do |namespace|
namespace_file_name = "#{self.tmp_dir}/#{namespace}-namespace-k8s-api-output.json"
Log.info { "namespace_file : #{namespace_file_name}" }
Expand All @@ -76,14 +94,25 @@ module Operator
JSON.parse(file)
end

# check if json empty
if json.as_h.empty?
Log.warn { "Namespace #{namespace} json response empty. Likely already removed" }
results[namespace] = {status: Process::Status.new(0), output: "", error: ""} #empty result
delete = true
break
end

json.as_h.delete("spec")

Log.info { "#{json.to_json}" }

File.write(namespace_file_name, "#{json.to_json}")
Log.info { "Uninstall Namespace Finalizer #{namespace_file_name}" }

result = KubectlClient::Replace.command("--raw \"/api/v1/namespace/#{namespace}/finalize\" -f #{namespace_file_name}")
result = KubectlClient::Replace.command("--raw \"/api/v1/namespaces/#{namespace}/finalize\" -f #{namespace_file_name}")
results[namespace] = result

# TODO: figure out a way to distuguish between NotFound because of missplled namespace and NotFound because the namespace was deleted
if result[:status].success? || result[:error].includes?("NotFound")
delete = true
end
Expand All @@ -98,9 +127,8 @@ module Operator

File.delete(namespace_file_name)
end
end

def cleanup_olm
results
end

def self.get_all_subscription_names(resources)
Expand Down
1 change: 1 addition & 0 deletions src/tasks/utils/utils.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require "semantic_version"
require "./dockerd.cr"
require "./kyverno.cr"
require "./http_helper.cr"
require "./operator.cr"
require "ecr"

module ShellCmd
Expand Down
1 change: 0 additions & 1 deletion src/tasks/workload/configuration.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ require "colorize"
require "totem"
require "json"
require "../utils/utils.cr"
require "../../../utils/operator.cr"

rolling_version_change_test_names = ["rolling_update", "rolling_downgrade", "rolling_version_change"]

Expand Down

0 comments on commit 9a2c877

Please sign in to comment.