Skip to content

Commit

Permalink
refactor: server side apply over create / update (#3075)
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
Signed-off-by: Austin Abro <[email protected]>
  • Loading branch information
AustinAbro321 authored Nov 6, 2024
1 parent df1ef35 commit e0040ab
Show file tree
Hide file tree
Showing 20 changed files with 376 additions and 449 deletions.
2 changes: 1 addition & 1 deletion src/internal/agent/hooks/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type admissionTest struct {

func createTestClientWithZarfState(ctx context.Context, t *testing.T, state *types.ZarfState) *cluster.Cluster {
t.Helper()
c := &cluster.Cluster{Clientset: fake.NewSimpleClientset()}
c := &cluster.Cluster{Clientset: fake.NewClientset()}
stateData, err := json.Marshal(state)
require.NoError(t, err)

Expand Down
54 changes: 9 additions & 45 deletions src/internal/packager/helm/post-render.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bytes"
"context"
"fmt"
"maps"
"os"
"path/filepath"
"slices"
Expand All @@ -21,11 +20,11 @@ import (
"github.com/zarf-dev/zarf/src/pkg/utils"
"github.com/zarf-dev/zarf/src/types"
"helm.sh/helm/v3/pkg/releaseutil"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/restmapper"
"sigs.k8s.io/yaml"

corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -164,49 +163,14 @@ func (r *renderer) adoptAndUpdateNamespaces(ctx context.Context) error {
if err != nil {
return err
}
// TODO: Refactor as error is not checked instead of checking for not found error.
currentRegistrySecret, _ := c.Clientset.CoreV1().Secrets(name).Get(ctx, config.ZarfImagePullSecretName, metav1.GetOptions{})
sameSecretData := maps.EqualFunc(currentRegistrySecret.Data, validRegistrySecret.Data, func(v1, v2 []byte) bool { return bytes.Equal(v1, v2) })
if currentRegistrySecret.Name != config.ZarfImagePullSecretName || !sameSecretData {
err := func() error {
_, err := c.Clientset.CoreV1().Secrets(validRegistrySecret.Namespace).Create(ctx, validRegistrySecret, metav1.CreateOptions{})
if err != nil && !kerrors.IsAlreadyExists(err) {
return err
}
if err == nil {
return nil
}
_, err = c.Clientset.CoreV1().Secrets(validRegistrySecret.Namespace).Update(ctx, validRegistrySecret, metav1.UpdateOptions{})
if err != nil {
return err
}
return nil
}()
if err != nil {
message.WarnErrf(err, "Problem creating registry secret for the %s namespace", name)
l.Warn("problem creating registry secret", "namespace", name, "error", err.Error())
}

// Create or update the zarf git server secret
gitServerSecret := c.GenerateGitPullCreds(name, config.ZarfGitServerSecretName, r.state.GitServer)
err = func() error {
_, err := c.Clientset.CoreV1().Secrets(gitServerSecret.Namespace).Create(ctx, gitServerSecret, metav1.CreateOptions{})
if err != nil && !kerrors.IsAlreadyExists(err) {
return err
}
if err == nil {
return nil
}
_, err = c.Clientset.CoreV1().Secrets(gitServerSecret.Namespace).Update(ctx, gitServerSecret, metav1.UpdateOptions{})
if err != nil {
return err
}
return nil
}()
if err != nil {
message.WarnErrf(err, "Problem creating git server secret for the %s namespace", name)
l.Warn("problem creating git server secret", "namespace", name, "error", err.Error())
}
_, err = c.Clientset.CoreV1().Secrets(*validRegistrySecret.Namespace).Apply(ctx, validRegistrySecret, metav1.ApplyOptions{Force: true, FieldManager: cluster.FieldManagerName})
if err != nil {
return fmt.Errorf("problem applying registry secret for the %s namespace: %w", name, err)
}
gitServerSecret := c.GenerateGitPullCreds(name, config.ZarfGitServerSecretName, r.state.GitServer)
_, err = c.Clientset.CoreV1().Secrets(*gitServerSecret.Namespace).Apply(ctx, gitServerSecret, metav1.ApplyOptions{Force: true, FieldManager: cluster.FieldManagerName})
if err != nil {
return fmt.Errorf("problem applying git server secret for the %s namespace: %w", name, err)
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion src/internal/packager2/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestPackageFromSourceOrCluster(t *testing.T) {
require.Equal(t, "test", pkg.Metadata.Name)

c := &cluster.Cluster{
Clientset: fake.NewSimpleClientset(),
Clientset: fake.NewClientset(),
}
_, err = c.RecordPackageDeployment(ctx, pkg, nil)
require.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions src/pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const (
DefaultTimeout = 30 * time.Second
// AgentLabel is used to give instructions to the Zarf agent
AgentLabel = "zarf.dev/agent"
// FieldManagerName is the field manager used during server side apply
FieldManagerName = "zarf"
)

// Cluster Zarf specific cluster management functions.
Expand Down
Loading

0 comments on commit e0040ab

Please sign in to comment.