Skip to content

Commit

Permalink
Merge branch 'main' into add-variable-unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Rodriguez committed May 23, 2024
2 parents dbcb501 + f003c94 commit ff7439a
Show file tree
Hide file tree
Showing 21 changed files with 771 additions and 463 deletions.
14 changes: 11 additions & 3 deletions src/cmd/common/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
package common

import (
"fmt"
"io"
"os"
"time"

"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/config/lang"
Expand Down Expand Up @@ -51,14 +53,20 @@ func SetupCLI() {
}

if !config.SkipLogFile {
logFile, err := message.UseLogFile("")
ts := time.Now().Format("2006-01-02-15-04-05")

f, err := os.CreateTemp("", fmt.Sprintf("zarf-%s-*.log", ts))
if err != nil {
message.WarnErr(err, "Error creating a log file in a temporary directory")
return
}
logFile, err := message.UseLogFile(f)
if err != nil {
message.WarnErr(err, "Error saving a log file to a temporary directory")
return
}

pterm.SetDefaultOutput(io.MultiWriter(os.Stderr, logFile))
location := message.LogFileLocation()
message.Notef("Saving log file to %s", location)
message.Notef("Saving log file to %s", f.Name())
}
}
121 changes: 86 additions & 35 deletions src/pkg/cluster/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/google/go-containerregistry/pkg/crane"
"github.com/mholt/archiver/v3"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -150,13 +151,28 @@ func (c *Cluster) StopInjectionMadness(ctx context.Context) error {
}

// Remove the configmaps
labelMatch := map[string]string{"zarf-injector": "payload"}
if err := c.DeleteConfigMapsByLabel(ctx, ZarfNamespaceName, labelMatch); err != nil {
selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
MatchLabels: map[string]string{
"zarf-injector": "payload",
},
})
if err != nil {
return err
}
listOpts := metav1.ListOptions{
LabelSelector: selector.String(),
}
err = c.Clientset.CoreV1().ConfigMaps(ZarfNamespaceName).DeleteCollection(ctx, metav1.DeleteOptions{}, listOpts)
if err != nil {
return err
}

// Remove the injector service
return c.DeleteService(ctx, ZarfNamespaceName, "zarf-injector")
err = c.Clientset.CoreV1().Services(ZarfNamespaceName).Delete(ctx, "zarf-injector", metav1.DeleteOptions{})
if err != nil {
return err
}
return nil
}

func (c *Cluster) loadSeedImages(imagesDir, seedImagesDir string, injectorSeedSrcs []string, spinner *message.Spinner) ([]transform.Image, error) {
Expand Down Expand Up @@ -226,16 +242,26 @@ func (c *Cluster) createPayloadConfigMaps(ctx context.Context, seedImagesDir, ta
// Create a cat-friendly filename
fileName := fmt.Sprintf("zarf-payload-%03d", idx)

// Store the binary data
configData := map[string][]byte{
fileName: data,
}

spinner.Updatef("Adding archive binary configmap %d of %d to the cluster", idx+1, chunkCount)

// Attempt to create the configmap in the cluster
if _, err = c.ReplaceConfigmap(ctx, ZarfNamespaceName, fileName, configData); err != nil {
return configMaps, "", err
// TODO: Replace with create or update.
err := c.Clientset.CoreV1().ConfigMaps(ZarfNamespaceName).Delete(ctx, fileName, metav1.DeleteOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return nil, "", err
}
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: fileName,
Namespace: ZarfNamespaceName,
},
BinaryData: map[string][]byte{
fileName: data,
},
}
_, err = c.Clientset.CoreV1().ConfigMaps(ZarfNamespaceName).Create(ctx, cm, metav1.CreateOptions{})
if err != nil {
return nil, "", err
}

// Add the configmap to the configmaps slice for later usage in the pod
Expand Down Expand Up @@ -286,40 +312,65 @@ func (c *Cluster) injectorIsReady(ctx context.Context, seedImages []transform.Im
}

func (c *Cluster) createInjectorConfigMap(ctx context.Context, binaryPath string) error {
var err error
configData := make(map[string][]byte)

// Add the injector binary data to the configmap
if configData["zarf-injector"], err = os.ReadFile(binaryPath); err != nil {
name := "rust-binary"
// TODO: Replace with a create or update.
err := c.Clientset.CoreV1().ConfigMaps(ZarfNamespaceName).Delete(ctx, name, metav1.DeleteOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return err
}

// Try to delete configmap silently
_ = c.DeleteConfigmap(ctx, ZarfNamespaceName, "rust-binary")

// Attempt to create the configmap in the cluster
if _, err = c.CreateConfigmap(ctx, ZarfNamespaceName, "rust-binary", configData); err != nil {
b, err := os.ReadFile(binaryPath)
if err != nil {
return err
}
configData := map[string][]byte{
"zarf-injector": b,
}
configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ZarfNamespaceName,
},
BinaryData: configData,
}
_, err = c.Clientset.CoreV1().ConfigMaps(configMap.Namespace).Create(ctx, configMap, metav1.CreateOptions{})
if err != nil {
return err
}

return nil
}

func (c *Cluster) createService(ctx context.Context) (*corev1.Service, error) {
service := c.GenerateService(ZarfNamespaceName, "zarf-injector")

service.Spec.Type = corev1.ServiceTypeNodePort
service.Spec.Ports = append(service.Spec.Ports, corev1.ServicePort{
Port: int32(5000),
})
service.Spec.Selector = map[string]string{
"app": "zarf-injector",
svc := &corev1.Service{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "Service",
},
ObjectMeta: metav1.ObjectMeta{
Name: "zarf-injector",
Namespace: ZarfNamespaceName,
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeNodePort,
Ports: []corev1.ServicePort{
{
Port: int32(5000),
},
},
Selector: map[string]string{
"app": "zarf-injector",
},
},
}

// Attempt to purse the service silently
_ = c.DeleteService(ctx, ZarfNamespaceName, "zarf-injector")

return c.CreateService(ctx, service)
// TODO: Replace with create or update
err := c.Clientset.CoreV1().Services(svc.Namespace).Delete(ctx, svc.Name, metav1.DeleteOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return nil, err
}
svc, err = c.Clientset.CoreV1().Services(svc.Namespace).Create(ctx, svc, metav1.CreateOptions{})
if err != nil {
return nil, err
}
return svc, nil
}

// buildInjectionPod return a pod for injection with the appropriate containers to perform the injection.
Expand Down
2 changes: 2 additions & 0 deletions src/pkg/cluster/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (c *Cluster) GenerateGitPullCreds(namespace, name string, gitServerInfo typ
}

// UpdateZarfManagedImageSecrets updates all Zarf-managed image secrets in all namespaces based on state
// TODO: Refactor to return errors properly.
func (c *Cluster) UpdateZarfManagedImageSecrets(ctx context.Context, state *types.ZarfState) {
spinner := message.NewProgressSpinner("Updating existing Zarf-managed image secrets")
defer spinner.Stop()
Expand Down Expand Up @@ -109,6 +110,7 @@ func (c *Cluster) UpdateZarfManagedImageSecrets(ctx context.Context, state *type
}

// UpdateZarfManagedGitSecrets updates all Zarf-managed git secrets in all namespaces based on state
// TODO: Refactor to return errors properly.
func (c *Cluster) UpdateZarfManagedGitSecrets(ctx context.Context, state *types.ZarfState) {
spinner := message.NewProgressSpinner("Updating existing Zarf-managed git secrets")
defer spinner.Stop()
Expand Down
76 changes: 76 additions & 0 deletions src/pkg/cluster/secrets_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

package cluster

import (
"testing"

"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/defenseunicorns/zarf/src/pkg/k8s"
"github.com/defenseunicorns/zarf/src/types"
)

func TestGenerateRegistryPullCreds(t *testing.T) {
c := &Cluster{}
ri := types.RegistryInfo{
PushUsername: "push-user",
PushPassword: "push-password",
PullUsername: "pull-user",
PullPassword: "pull-password",
Address: "example.com",
}
secret := c.GenerateRegistryPullCreds("foo", "bar", ri)
expectedSecret := corev1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: "bar",
Namespace: "foo",
Labels: map[string]string{
k8s.ZarfManagedByLabel: "zarf",
},
},
Type: corev1.SecretTypeDockerConfigJson,
Data: map[string][]byte{
".dockerconfigjson": []byte(`{"auths":{"example.com":{"auth":"cHVsbC11c2VyOnB1bGwtcGFzc3dvcmQ="}}}`),
},
}
require.Equal(t, expectedSecret, *secret)
}

func TestGenerateGitPullCreds(t *testing.T) {
c := &Cluster{}
gi := types.GitServerInfo{
PushUsername: "push-user",
PushPassword: "push-password",
PullUsername: "pull-user",
PullPassword: "pull-password",
}
secret := c.GenerateGitPullCreds("foo", "bar", gi)
expectedSecret := corev1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: "bar",
Namespace: "foo",
Labels: map[string]string{
k8s.ZarfManagedByLabel: "zarf",
},
},
Type: corev1.SecretTypeOpaque,
Data: map[string][]byte{},
StringData: map[string]string{
"username": "pull-user",
"password": "pull-password",
},
}
require.Equal(t, expectedSecret, *secret)
}
24 changes: 23 additions & 1 deletion src/pkg/cluster/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/fatih/color"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/defenseunicorns/pkg/helpers"
Expand Down Expand Up @@ -115,7 +116,28 @@ func (c *Cluster) InitZarfState(ctx context.Context, initOptions types.ZarfInitO
// The default SA is required for pods to start properly.
saCtx, cancel := context.WithTimeout(ctx, 2*time.Minute)
defer cancel()
if _, err := c.WaitForServiceAccount(saCtx, ZarfNamespaceName, "default"); err != nil {
err = func(ctx context.Context, ns, name string) error {
timer := time.NewTimer(0)
defer timer.Stop()
for {
select {
case <-ctx.Done():
return fmt.Errorf("failed to get service account %s/%s: %w", ns, name, ctx.Err())
case <-timer.C:
_, err := c.Clientset.CoreV1().ServiceAccounts(ns).Get(ctx, name, metav1.GetOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return err
}
if kerrors.IsNotFound(err) {
c.Log("Service account %s/%s not found, retrying...", ns, name)
timer.Reset(1 * time.Second)
continue
}
return nil
}
}
}(saCtx, ZarfNamespaceName, "default")
if err != nil {
return fmt.Errorf("unable get default Zarf service account: %w", err)
}

Expand Down
Loading

0 comments on commit ff7439a

Please sign in to comment.