From 20796d45a223273ff3b2ef67289a403a383735ad Mon Sep 17 00:00:00 2001 From: Kevin Downey Date: Fri, 2 Jun 2023 19:59:14 -0700 Subject: [PATCH] Local build fixes (#250) * Remove deprecated ioutil functions * Use module dir for code gen * Ignore *.log files * Add more time to AfterSuite * Add more tests to controller suite --------- Signed-off-by: kevdowney --- .gitignore | 1 + Makefile | 3 +- controllers/addon_controller_test.go | 55 +++++++++++++++++++++++++--- controllers/suite_test.go | 2 +- pkg/addonctl/addonctl.go | 9 ++--- test-bdd/main_test.go | 4 +- test-bdd/testutil/helpers.go | 3 +- 7 files changed, 61 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 289bd2bf..2ba5717c 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,4 @@ bin/ dist/ +*.log diff --git a/Makefile b/Makefile index 0d1be6b0..d984a08e 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ KUBERNETES_LOCAL_CLUSTER_VERSION ?= --image=kindest/node:v1.24.7 GIT_COMMIT := $(shell git rev-parse --short HEAD) BUILD_DATE := $(shell date -u +%Y-%m-%dT%H:%M:%SZ) PKGS := $(shell go list ./...|grep -v test-) +MODULE := $(shell go list -m) # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) ifeq (,$(shell go env GOBIN)) @@ -96,7 +97,7 @@ vet: # Generate code .PHONY: generate generate: controller-gen types ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. - $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..." + $(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="$(MODULE)" # generates many other files (listers, informers, client etc). api/addon/v1alpha1/zz_generated.deepcopy.go: $(TYPES) diff --git a/controllers/addon_controller_test.go b/controllers/addon_controller_test.go index 2f68138b..a8d4bb85 100644 --- a/controllers/addon_controller_test.go +++ b/controllers/addon_controller_test.go @@ -16,7 +16,7 @@ package controllers import ( "context" "fmt" - "io/ioutil" + "os" "time" "github.com/keikoproj/addon-manager/api/addon" @@ -55,7 +55,7 @@ var _ = Describe("AddonController", func() { Context("Addon CR is created", func() { It("Creating a new Addon instance", func() { - addonYaml, err := ioutil.ReadFile("../docs/examples/clusterautoscaler.yaml") + addonYaml, err := os.ReadFile("../docs/examples/clusterautoscaler.yaml") Expect(err).ToNot(HaveOccurred()) instance, err = parseAddonYaml(addonYaml) @@ -110,7 +110,7 @@ var _ = Describe("AddonController", func() { By("Verify changing addon spec generates new checksum") Expect(instance.Status.Checksum).ShouldNot(BeIdenticalTo(oldCheckSum)) - By("Verify addon has workflows generated with new checksum name") + By("Verify addon has prereqs workflow generated with new checksum name") wfName := instance.GetFormattedWorkflowName(v1alpha1.Prereqs) var wfv1Key = types.NamespacedName{Name: wfName, Namespace: addonNamespace} Eventually(func() error { @@ -118,7 +118,52 @@ var _ = Describe("AddonController", func() { }, timeout).Should(Succeed()) Expect(wfv1.GetName()).Should(Equal(wfName)) - By("Verify deleting workflows triggers reconcile and doesn't regenerate workflows again") + By("Verify addon still has pending status") + err = k8sClient.Get(context.TODO(), addonKey, instance) + Expect(err).NotTo(HaveOccurred()) + Expect(instance.Status.Lifecycle.Installed).Should(Equal(v1alpha1.Pending)) + + By("Verify addon prereqs status completed after prereqs workflow is completed") + wfv1.UnstructuredContent()["status"] = map[string]interface{}{ + "phase": "Succeeded", + } + err = k8sClient.Update(context.TODO(), wfv1) + Expect(err).NotTo(HaveOccurred()) + Eventually(func() error { + if err := k8sClient.Get(context.TODO(), addonKey, instance); err != nil { + return err + } + if instance.Status.Lifecycle.Prereqs == v1alpha1.Succeeded && instance.Status.Lifecycle.Installed == v1alpha1.Pending { + return nil + } + return fmt.Errorf("addon prereqs|install status(%s|%s) is not succeeded|pending", instance.Status.Lifecycle.Prereqs, instance.Status.Lifecycle.Installed) + }, timeout).Should(Succeed()) + + By("Verify addon has install workflow generated with new checksum name") + wfName = instance.GetFormattedWorkflowName(v1alpha1.Install) + wfv1Key = types.NamespacedName{Name: wfName, Namespace: addonNamespace} + Eventually(func() error { + return k8sClient.Get(context.TODO(), wfv1Key, wfv1) + }, timeout).Should(Succeed()) + Expect(wfv1.GetName()).Should(Equal(wfName)) + + By("Verify addon install status completed after install workflow is completed") + wfv1.UnstructuredContent()["status"] = map[string]interface{}{ + "phase": "Succeeded", + } + err = k8sClient.Update(context.TODO(), wfv1) + Expect(err).NotTo(HaveOccurred()) + Eventually(func() error { + if err := k8sClient.Get(context.TODO(), addonKey, instance); err != nil { + return err + } + if instance.Status.Lifecycle.Installed == v1alpha1.Succeeded { + return nil + } + return fmt.Errorf("addon install status(%s) is not succeeded", instance.Status.Lifecycle.Installed) + }, timeout).Should(Succeed()) + + By("Verify deleting workflows triggers reconcile but doesn't regenerate workflows again after completed") Expect(k8sClient.Delete(context.TODO(), wfv1)).To(Succeed()) Consistently(func() error { if apierrors.IsNotFound(k8sClient.Get(context.TODO(), wfv1Key, wfv1)) { @@ -197,7 +242,7 @@ var _ = Describe("AddonController", func() { }) It("Creating a new Addon instance", func() { - addonYaml, err := ioutil.ReadFile("../docs/examples/eventrouter.yaml") + addonYaml, err := os.ReadFile("../docs/examples/eventrouter.yaml") Expect(err).ToNot(HaveOccurred()) instance, err = parseAddonYaml(addonYaml) diff --git a/controllers/suite_test.go b/controllers/suite_test.go index 866174b7..c4dbf83a 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -103,4 +103,4 @@ var _ = AfterSuite(func() { By("tearing down the test environment") err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) -}) +}, 60) diff --git a/pkg/addonctl/addonctl.go b/pkg/addonctl/addonctl.go index e889e385..bce24d2d 100644 --- a/pkg/addonctl/addonctl.go +++ b/pkg/addonctl/addonctl.go @@ -18,7 +18,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "os" "path/filepath" "regexp" @@ -342,14 +341,14 @@ func extractResources(prereqsPath, installPath string) error { case err != nil: return err case fi.IsDir(): - files, err := ioutil.ReadDir(path) + files, err := os.ReadDir(path) if err != nil { return errors.Wrapf(err, "failed to read dir path %s", path) } for _, f := range files { switch { case strings.HasSuffix(f.Name(), ".py"): - data, err := ioutil.ReadFile(f.Name()) + data, err := os.ReadFile(f.Name()) if err != nil { return fmt.Errorf("unable to read file %s", f.Name()) } @@ -373,7 +372,7 @@ func extractResources(prereqsPath, installPath string) error { // it's a file switch { case strings.HasSuffix(fi.Name(), ".py"): - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { return fmt.Errorf("unable to read file %s", fi.Name()) } @@ -415,7 +414,7 @@ func parseSecrets(raw string) error { // best way to write parsing functions? take no params and work on global variables, or take and modify the global params (need to pass in pointers in that case) func parseResources(filename, stepName string) error { - rawBytes, err := ioutil.ReadFile(filename) + rawBytes, err := os.ReadFile(filename) if err != nil { return errors.Wrapf(err, "failed to read file %s", filename) } diff --git a/test-bdd/main_test.go b/test-bdd/main_test.go index 1b8a2e61..97976df2 100644 --- a/test-bdd/main_test.go +++ b/test-bdd/main_test.go @@ -17,7 +17,7 @@ package main_test import ( "context" "fmt" - "io/ioutil" + "os" "testing" addonmgrv1alpha1 "github.com/keikoproj/addon-manager/api/addon/v1alpha1" @@ -70,7 +70,7 @@ var _ = Describe("Addon Mgr should install CRD and Addon correctly", func() { // Setup CRD It("should create CRD", func() { crdsRoot := "../config/crd/bases" - files, err := ioutil.ReadDir(crdsRoot) + files, err := os.ReadDir(crdsRoot) if err != nil { Fail(fmt.Sprintf("failed to read crd path. %v", err)) } diff --git a/test-bdd/testutil/helpers.go b/test-bdd/testutil/helpers.go index a17d6b57..8a95a313 100644 --- a/test-bdd/testutil/helpers.go +++ b/test-bdd/testutil/helpers.go @@ -17,7 +17,6 @@ package testutil import ( "context" "fmt" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -95,7 +94,7 @@ func ConcatonateList(list []string, delimiter string) string { // ReadFile reads the raw content from a file path func ReadFile(path string) ([]byte, error) { - f, err := ioutil.ReadFile(path) + f, err := os.ReadFile(path) if err != nil { fmt.Printf("failed to read file %v", path) return nil, err