Skip to content

Commit

Permalink
apply the split up manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterp committed Feb 8, 2024
1 parent 177e250 commit 2dc0d5f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 22 deletions.
15 changes: 11 additions & 4 deletions internal/integ/cluster/teardown.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cluster

import (
"fmt"
"log/slog"

"github.com/exoscale/exoscale/csi-driver/internal/integ/flags"
)
Expand Down Expand Up @@ -45,12 +46,18 @@ func (c *Cluster) TearDown() error {
}

func (c *Cluster) tearDownCSI() error {
err := c.K8s.DeleteManifest(c.exoV2Context, mainManifest)
if err != nil {
return err
var finalErr error = nil

for _, manifestPath := range allManifests {
err := c.K8s.DeleteManifest(c.exoV2Context, manifestDir+manifestPath)
if err != nil {
slog.Error("failed to delete manifest", "manifest", manifestPath)

finalErr = fmt.Errorf("errors while deleting manifests")
}
}

return err
return finalErr

// TODO (sauterp) reenable once we have a non-legacy key in GH
// return c.deleteAPIKeyAndRole()
Expand Down
32 changes: 28 additions & 4 deletions internal/integ/cluster/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,25 @@ const (
)

var (
mainManifest = util.GetRepoRootDir() + "deployment/exoscale-csi.yaml"
manifestDir = util.GetRepoRootDir() + "deployment/"
controllerRBACManifest = "controller-rbac.yaml"
controllerManifest = "controller.yaml"
crdsManifest = "crds.yaml"
csiDriverManifest = "csi-driver.yaml"
nodeDriverRBACManifest = "node-driver-rbac.yaml"
nodeDriverManifest = "node-driver.yaml"
storageClassManifest = "storage-class.yaml"
volumeSnapshotClassManifest = "volume-snapshot-class.yaml"

allManifests = []string{
controllerRBACManifest,
controllerManifest,
csiDriverManifest,
nodeDriverRBACManifest,
nodeDriverManifest,
storageClassManifest,
volumeSnapshotClassManifest,
}
)

func ptr[T any](v T) *T {
Expand Down Expand Up @@ -241,9 +259,15 @@ func (c *Cluster) applyCSI() error {
}
}

err := c.K8s.ApplyManifest(c.exoV2Context, mainManifest)
if err != nil {
return fmt.Errorf("error applying CSI manifests: %w", err)
if *flags.Image != "" {
slog.Info("testing CSI image", "path", *flags.Image)
}

for _, manifestPath := range allManifests {
err := c.K8s.ApplyManifest(c.exoV2Context, manifestDir+manifestPath)
if err != nil {
return fmt.Errorf("error applying CSI manifest: %q %w", manifestPath, err)
}
}

// TODO(sauterp) this shouldn't be necessary anymore once the CSI addon is available.
Expand Down
32 changes: 18 additions & 14 deletions internal/integ/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ func (k *K8S) ApplyManifest(ctx context.Context, file string) error {

fileContentStr := string(fileContent)
if *flags.Image != "" {
slog.Info("testing CSI image", "path", *flags.Image)

fileContentStr = strings.ReplaceAll(fileContentStr, `exoscale/csi-driver:latest`, *flags.Image)
}

Expand Down Expand Up @@ -211,6 +209,15 @@ func (k *K8S) PrintEvents(ctx context.Context, ns string) {
}
}

func (k *K8S) UpdateResourceList() {
_, newResourceList, err := k.DiscoveryClient.ServerGroupsAndResources()
if err != nil {
slog.Warn("failed to update resource list", "err", err)
} else {
k.ResourceList = newResourceList
}
}

func (k *K8S) applyManifest(ctx context.Context, manifest []byte) error {
decoder := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
obj := &unstructured.Unstructured{}
Expand All @@ -219,20 +226,20 @@ func (k *K8S) applyManifest(ctx context.Context, manifest []byte) error {
return err
}

rrr := k.findResource(obj.GetKind())
if rrr == nil {
res := k.findResource(obj.GetKind())
if res == nil {
return nil
}

gvr := gvk.GroupVersion().WithResource(rrr.Name)
gvr := gvk.GroupVersion().WithResource(res.Name)
namespace := ""
if rrr.Namespaced {
if res.Namespaced {
namespace = obj.GetNamespace()
go k.PrintEvents(ctx, namespace)
}
resourceInterface := k.DynamicClient.Resource(gvr).Namespace(namespace)

res, err := resourceInterface.Get(ctx, obj.GetName(), metav1.GetOptions{})
resIf, err := resourceInterface.Get(ctx, obj.GetName(), metav1.GetOptions{})
if err != nil {
slog.Info("creating", "resource", gvr, "name", obj.GetName())

Expand All @@ -244,7 +251,7 @@ func (k *K8S) applyManifest(ctx context.Context, manifest []byte) error {
} else {
slog.Info("updating", "resource", gvr, "name", obj.GetName())

obj.SetResourceVersion(res.GetResourceVersion())
obj.SetResourceVersion(resIf.GetResourceVersion())

// If it exists, update it
retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
Expand All @@ -256,12 +263,9 @@ func (k *K8S) applyManifest(ctx context.Context, manifest []byte) error {
}
}

_, newResourceList, err := k.DiscoveryClient.ServerGroupsAndResources()
if err != nil {
slog.Warn("failed to update resource list", "err", err)
} else {
k.ResourceList = newResourceList
}
// The resources we just applied need to be fetched from the server
// so that the client can apply manifests that depend on these.
k.UpdateResourceList()

return nil
}
Expand Down

0 comments on commit 2dc0d5f

Please # to comment.