Skip to content

Commit

Permalink
Merge branch 'ignore-file-not-found-errors' of github.com:zarf-dev/za…
Browse files Browse the repository at this point in the history
…rf into ignore-file-not-found-errors
  • Loading branch information
AustinAbro321 committed Aug 5, 2024
2 parents 5719b5a + 9a6d392 commit 6408d97
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/pkg/cluster/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (c *Cluster) GenerateRegistryPullCreds(ctx context.Context, namespace, name
// Convert to JSON
dockerConfigData, err := json.Marshal(dockerConfigJSON)
if err != nil {
return nil, fmt.Errorf("Unable to marshal the .dockerconfigjson secret data for the image pull secret: %w", err)
return nil, fmt.Errorf("unable to marshal the .dockerconfigjson secret data for the image pull secret: %w", err)
}

secretDockerConfig := &corev1.Secret{
Expand Down
21 changes: 9 additions & 12 deletions src/pkg/cluster/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ const (

// InitZarfState initializes the Zarf state with the given temporary directory and init configs.
func (c *Cluster) InitZarfState(ctx context.Context, initOptions types.ZarfInitOptions) error {
var distro string

spinner := message.NewProgressSpinner("Gathering cluster state information")
defer spinner.Stop()

// Attempt to load an existing state prior to init.
// NOTE: We are ignoring the error here because we don't really expect a state to exist yet.
spinner.Updatef("Checking cluster for existing Zarf deployment")
state, _ := c.LoadZarfState(ctx)
state, err := c.LoadZarfState(ctx)
if err != nil && !kerrors.IsNotFound(err) {
return fmt.Errorf("failed to check for existing state: %w", err)
}

// If state is nil, this is a new cluster.
if state == nil {
Expand All @@ -52,7 +53,7 @@ func (c *Cluster) InitZarfState(ctx context.Context, initOptions types.ZarfInitO

if initOptions.ApplianceMode {
// If the K3s component is being deployed, skip distro detection.
distro = DistroIsK3s
state.Distro = DistroIsK3s
state.ZarfAppliance = true
} else {
// Otherwise, trying to detect the K8s distro type.
Expand All @@ -67,16 +68,13 @@ func (c *Cluster) InitZarfState(ctx context.Context, initOptions types.ZarfInitO
if err != nil {
return err
}
distro = detectDistro(nodeList.Items[0], namespaceList.Items)
state.Distro = detectDistro(nodeList.Items[0], namespaceList.Items)
}

if distro != DistroIsUnknown {
spinner.Updatef("Detected K8s distro %s", distro)
if state.Distro != DistroIsUnknown {
spinner.Updatef("Detected K8s distro %s", state.Distro)
}

// Defaults
state.Distro = distro

// Setup zarf agent PKI
agentTLS, err := pki.GeneratePKI(config.ZarfAgentHost)
if err != nil {
Expand All @@ -100,8 +98,7 @@ func (c *Cluster) InitZarfState(ctx context.Context, initOptions types.ZarfInitO
namespaceCopy := namespace
_, err := c.Clientset.CoreV1().Namespaces().Update(ctx, &namespaceCopy, metav1.UpdateOptions{})
if err != nil {
// This is not a hard failure, but we should log it.
message.WarnErrf(err, "Unable to mark the namespace %s as ignored by Zarf Agent", namespace.Name)
return fmt.Errorf("unable to mark the namespace %s as ignored by Zarf Agent: %w", namespace.Name, err)
}
}

Expand Down
58 changes: 56 additions & 2 deletions src/pkg/cluster/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cluster

import (
"context"
"encoding/json"
"fmt"
"testing"
"time"
Expand All @@ -23,6 +24,24 @@ import (
)

func TestInitZarfState(t *testing.T) {
emptyState := types.ZarfState{}
emptyStateData, err := json.Marshal(emptyState)
require.NoError(t, err)

existingState := types.ZarfState{
Distro: DistroIsK3d,
RegistryInfo: types.RegistryInfo{
PushUsername: "push-user",
PullUsername: "pull-user",
Address: "address",
NodePort: 1,
Secret: "secret",
},
}

existingStateData, err := json.Marshal(existingState)
require.NoError(t, err)

tests := []struct {
name string
initOpts types.ZarfInitOptions
Expand Down Expand Up @@ -85,6 +104,34 @@ func TestInitZarfState(t *testing.T) {
},
},
},
{
name: "empty Zarf state exists",
nodes: []corev1.Node{
{
ObjectMeta: metav1.ObjectMeta{
Name: "node",
},
},
},
namespaces: []corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: ZarfNamespaceName,
},
},
},
secrets: []corev1.Secret{
{
ObjectMeta: metav1.ObjectMeta{
Namespace: ZarfNamespaceName,
Name: ZarfStateSecretName,
},
Data: map[string][]byte{
ZarfStateDataKey: emptyStateData,
},
},
},
},
{
name: "Zarf state exists",
nodes: []corev1.Node{
Expand All @@ -107,6 +154,9 @@ func TestInitZarfState(t *testing.T) {
Namespace: ZarfNamespaceName,
Name: ZarfStateSecretName,
},
Data: map[string][]byte{
ZarfStateDataKey: existingStateData,
},
},
},
},
Expand Down Expand Up @@ -158,11 +208,15 @@ func TestInitZarfState(t *testing.T) {
return
}
require.NoError(t, err)
state, err := cs.CoreV1().Secrets(ZarfNamespaceName).Get(ctx, ZarfStateSecretName, metav1.GetOptions{})
require.NoError(t, err)
require.Equal(t, map[string]string{"app.kubernetes.io/managed-by": "zarf"}, state.Labels)
if tt.secrets != nil {
return
}
zarfNs, err := cs.CoreV1().Namespaces().Get(ctx, ZarfNamespaceName, metav1.GetOptions{})
require.NoError(t, err)
require.Equal(t, map[string]string{"app.kubernetes.io/managed-by": "zarf"}, zarfNs.Labels)
_, err = cs.CoreV1().Secrets(zarfNs.Name).Get(ctx, ZarfStateSecretName, metav1.GetOptions{})
require.NoError(t, err)
for _, ns := range tt.namespaces {
if ns.Name == zarfNs.Name {
continue
Expand Down

0 comments on commit 6408d97

Please sign in to comment.