diff --git a/src/pkg/cluster/secrets.go b/src/pkg/cluster/secrets.go index 3cdeabe826..43c3402b64 100644 --- a/src/pkg/cluster/secrets.go +++ b/src/pkg/cluster/secrets.go @@ -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{ diff --git a/src/pkg/cluster/state.go b/src/pkg/cluster/state.go index 82ee49424b..d1807f8d81 100644 --- a/src/pkg/cluster/state.go +++ b/src/pkg/cluster/state.go @@ -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 init Zarf state: %w", err) + } // If state is nil, this is a new cluster. if state == nil { @@ -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. @@ -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 { @@ -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) } } diff --git a/src/pkg/cluster/state_test.go b/src/pkg/cluster/state_test.go index cf52d195dc..231eddc8ca 100644 --- a/src/pkg/cluster/state_test.go +++ b/src/pkg/cluster/state_test.go @@ -6,6 +6,7 @@ package cluster import ( "context" + "encoding/json" "fmt" "testing" "time" @@ -23,6 +24,25 @@ 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, + InternalRegistry: false, + Secret: "secret", + }, + } + + existingStateData, err := json.Marshal(existingState) + require.NoError(t, err) + tests := []struct { name string initOpts types.ZarfInitOptions @@ -85,6 +105,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{ @@ -107,6 +155,9 @@ func TestInitZarfState(t *testing.T) { Namespace: ZarfNamespaceName, Name: ZarfStateSecretName, }, + Data: map[string][]byte{ + ZarfStateDataKey: existingStateData, + }, }, }, }, @@ -158,11 +209,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