Skip to content

Commit

Permalink
Local build fixes (#250)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
kevdowney authored Jun 3, 2023
1 parent d77c404 commit 20796d4
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ bin/
dist/


*.log
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
55 changes: 50 additions & 5 deletions controllers/addon_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package controllers
import (
"context"
"fmt"
"io/ioutil"
"os"
"time"

"github.com/keikoproj/addon-manager/api/addon"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -110,15 +110,60 @@ 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 {
return k8sClient.Get(context.TODO(), wfv1Key, wfv1)
}, 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)) {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,4 @@ var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})
}, 60)
9 changes: 4 additions & 5 deletions pkg/addonctl/addonctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions test-bdd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package main_test
import (
"context"
"fmt"
"io/ioutil"
"os"
"testing"

addonmgrv1alpha1 "github.com/keikoproj/addon-manager/api/addon/v1alpha1"
Expand Down Expand Up @@ -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))
}
Expand Down
3 changes: 1 addition & 2 deletions test-bdd/testutil/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package testutil
import (
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 20796d4

Please sign in to comment.