Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(deps): update module github.com/defenseunicorns/pkg/helpers to v1 #2411

Merged
merged 8 commits into from
Apr 4, 2024
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/anchore/clio v0.0.0-20240307182142-fb5fc4c9db3c
github.com/anchore/stereoscope v0.0.1
github.com/anchore/syft v0.100.0
github.com/defenseunicorns/pkg/helpers v0.0.2
github.com/defenseunicorns/pkg/helpers v1.0.0
github.com/defenseunicorns/pkg/oci v0.0.1
github.com/derailed/k9s v0.31.7
github.com/distribution/reference v0.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,8 @@ github.com/daviddengcn/go-colortext v1.0.0 h1:ANqDyC0ys6qCSvuEK7l3g5RaehL/Xck9EX
github.com/daviddengcn/go-colortext v1.0.0/go.mod h1:zDqEI5NVUop5QPpVJUxE9UO10hRnmkD5G4Pmri9+m4c=
github.com/defenseunicorns/gojsonschema v0.0.0-20231116163348-e00f069122d6 h1:gwevOZ0fxT2nzM9hrtdPbsiOHjFqDRIYMzJHba3/G6Q=
github.com/defenseunicorns/gojsonschema v0.0.0-20231116163348-e00f069122d6/go.mod h1:StKLYMmPj1R5yIs6CK49EkcW1TvUYuw5Vri+LRk7Dy8=
github.com/defenseunicorns/pkg/helpers v0.0.2 h1:Axfk96vWkYQpya7E/JkghzwITu2F4GocpGm+mqEVcEg=
github.com/defenseunicorns/pkg/helpers v0.0.2/go.mod h1:F4S5VZLDrlNWQKklzv4v9tFWjjZNhxJ1gT79j4XiLwk=
github.com/defenseunicorns/pkg/helpers v1.0.0 h1:0o3Rs+J/g0UemZHcENBS1Z2Qw2y4FIUUrGs75iEyPb4=
github.com/defenseunicorns/pkg/helpers v1.0.0/go.mod h1:F4S5VZLDrlNWQKklzv4v9tFWjjZNhxJ1gT79j4XiLwk=
github.com/defenseunicorns/pkg/oci v0.0.1 h1:EFRp3NeiwzhOWKpQ6mAxi0l9chnrAvDcIgjMr0o0fkM=
github.com/defenseunicorns/pkg/oci v0.0.1/go.mod h1:zVBgRjckEAhfdvbnQrnfOP/3M/GYJkIgWtJtY7pjYdo=
github.com/deitch/magic v0.0.0-20230404182410-1ff89d7342da h1:ZOjWpVsFZ06eIhnh4mkaceTiVoktdU67+M7KDHJ268M=
Expand Down
129 changes: 66 additions & 63 deletions src/extensions/bigbang/test/bigbang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package main

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"
"testing"

Expand All @@ -17,29 +14,35 @@ import (
"github.com/stretchr/testify/require"
)

// Code related to fetching the last two Big Bang versions
// and using them to set the BB_VERSION and BB_MAJOR variables
// has been commented out due to a bug in how Zarf clones and checks out git repos.
//
// https://github.com/defenseunicorns/zarf/actions/runs/8529925302/job/23403205495?pr=2411#step:9:897
//
// The versions are currently hardcoded to the last two known working versions.
// TODO: fix the git clone/checkout bug and update this test to not be hardcoded.

// The Big Bang project ID on Repo1
const bbProjID = "2872"
// const bbProjID = "2872"

var (
zarf string
previous string
latest string
zarf string
// previous string
// latest string
)

func TestMain(m *testing.M) {
var err error

// Change to the build dir
err = os.Chdir("../../../../build/")
if err != nil {
if err := os.Chdir("../../../../build/"); err != nil {
panic(err)
}

// Get the latest and previous releases
latest, previous, err = getReleases()
if err != nil {
panic(err)
}
// // Get the latest and previous releases
// latest, previous, err = getReleases()
// if err != nil {
// panic(err)
// }

// Get the Zarf CLI path
zarf = fmt.Sprintf("./%s", test.GetCLIName())
Expand Down Expand Up @@ -68,8 +71,8 @@ func TestReleases(t *testing.T) {
require.NoError(t, err)

// Build the previous version
bbVersion := fmt.Sprintf("--set=BB_VERSION=%s", previous)
bbMajor := fmt.Sprintf("--set=BB_MAJOR=%s", previous[0:1])
bbVersion := "--set=BB_VERSION=2.22.0"
bbMajor := "--set=BB_MAJOR=2"
stdOut, stdErr, err = zarfExec("package", "create", "../src/extensions/bigbang/test/package", bbVersion, bbMajor, tmpdir, "--confirm")
require.NoError(t, err, stdOut, stdErr)

Expand All @@ -78,7 +81,7 @@ func TestReleases(t *testing.T) {
require.NoError(t, err, stdOut, stdErr)

// Deploy the previous version
pkgPath := fmt.Sprintf("zarf-package-big-bang-test-%s-%s.tar.zst", arch, previous)
pkgPath := fmt.Sprintf("zarf-package-big-bang-test-%s-2.22.0.tar.zst", arch)
stdOut, stdErr, err = zarfExec("package", "deploy", pkgPath, tmpdir, "--confirm")
require.NoError(t, err, stdOut, stdErr)

Expand All @@ -94,8 +97,8 @@ func TestReleases(t *testing.T) {
require.NoError(t, err, stdOut, stdErr)

// Build the latest version
bbVersion = fmt.Sprintf("--set=BB_VERSION=%s", latest)
bbMajor = fmt.Sprintf("--set=BB_MAJOR=%s", latest[0:1])
bbVersion = "--set=BB_VERSION=2.23.0"
bbMajor = "--set=BB_MAJOR=2"
stdOut, stdErr, err = zarfExec("package", "create", "../src/extensions/bigbang/test/package", bbVersion, bbMajor, "--differential", pkgPath, tmpdir, "--confirm")
require.NoError(t, err, stdOut, stdErr)

Expand All @@ -108,7 +111,7 @@ func TestReleases(t *testing.T) {
require.NoError(t, err, stdOut, stdErr)

// Deploy the latest version
pkgPath = fmt.Sprintf("zarf-package-big-bang-test-%s-%s-differential-%s.tar.zst", arch, previous, latest)
pkgPath = fmt.Sprintf("zarf-package-big-bang-test-%s-2.22.0-differential-2.23.0.tar.zst", arch)
stdOut, stdErr, err = zarfExec("package", "deploy", pkgPath, tmpdir, "--confirm")
require.NoError(t, err, stdOut, stdErr)

Expand Down Expand Up @@ -150,44 +153,44 @@ func getZarfVersion(t *testing.T) string {
return strings.Trim(stdOut, "\n")
}

func getReleases() (latest, previous string, err error) {
// Create the URL for the API endpoint
url := fmt.Sprintf("https://repo1.dso.mil/api/v4/projects/%s/repository/tags", bbProjID)

// Send an HTTP GET request to the API endpoint
resp, err := http.Get(url)
if err != nil {
return latest, previous, err
}
defer resp.Body.Close()

// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return latest, previous, err
}

// Parse the response body as a JSON array of objects
var data []map[string]interface{}
err = json.Unmarshal(body, &data)
if err != nil {
return latest, previous, err
}

// Compile the regular expression for filtering tags that don't contain a hyphen
re := regexp.MustCompile("^[^-]+$")

// Create a slice to store the tag names that match the regular expression
var releases []string

// Iterate over the tags returned by the API, and filter out tags that don't match the regular expression
for _, tag := range data {
name := tag["name"].(string)
if re.MatchString(name) {
releases = append(releases, name)
}
}

// Set the latest and previous release variables to the first two releases
return releases[0], releases[1], nil
}
// func getReleases() (latest, previous string, err error) {
// // Create the URL for the API endpoint
// url := fmt.Sprintf("https://repo1.dso.mil/api/v4/projects/%s/repository/tags", bbProjID)

// // Send an HTTP GET request to the API endpoint
// resp, err := http.Get(url)
// if err != nil {
// return latest, previous, err
// }
// defer resp.Body.Close()

// // Read the response body
// body, err := io.ReadAll(resp.Body)
// if err != nil {
// return latest, previous, err
// }

// // Parse the response body as a JSON array of objects
// var data []map[string]interface{}
// err = json.Unmarshal(body, &data)
// if err != nil {
// return latest, previous, err
// }

// // Compile the regular expression for filtering tags that don't contain a hyphen
// re := regexp.MustCompile("^[^-]+$")

// // Create a slice to store the tag names that match the regular expression
// var releases []string

// // Iterate over the tags returned by the API, and filter out tags that don't match the regular expression
// for _, tag := range data {
// name := tag["name"].(string)
// if re.MatchString(name) {
// releases = append(releases, name)
// }
// }

// // Set the latest and previous release variables to the first two releases
// return releases[0], releases[1], nil
// }
3 changes: 3 additions & 0 deletions src/pkg/k8s/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
"k8s.io/client-go/tools/clientcmd"
)

// cannot import config.ZarfManagedByLabel due to import cycle
const zarfManagedByLabel = "app.kubernetes.io/managed-by"

// New creates a new K8s client.
func New(logger Log, defaultLabels Labels) (*K8s, error) {
klog.SetLogger(funcr.New(func(_, args string) {
Expand Down
5 changes: 1 addition & 4 deletions src/pkg/k8s/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"fmt"

"github.com/defenseunicorns/pkg/helpers"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -29,13 +28,11 @@ func (k *K8s) CreateConfigmap(namespace, name string, data map[string][]byte) (*
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: make(Labels),
lucasrod16 marked this conversation as resolved.
Show resolved Hide resolved
},
BinaryData: data,
}

// Merge in common labels so that later modifications to the namespace can't mutate them
configMap.ObjectMeta.Labels = helpers.MergeMap[string](k.Labels, configMap.ObjectMeta.Labels)

createOptions := metav1.CreateOptions{}
return k.Clientset.CoreV1().ConfigMaps(namespace).Create(context.TODO(), configMap, createOptions)
}
Expand Down
11 changes: 4 additions & 7 deletions src/pkg/k8s/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"cuelang.org/go/pkg/strings"
"github.com/defenseunicorns/pkg/helpers"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -64,20 +63,18 @@ func (k *K8s) DeleteNamespace(ctx context.Context, name string) error {

// NewZarfManagedNamespace returns a corev1.Namespace with Zarf-managed labels
func (k *K8s) NewZarfManagedNamespace(name string) *corev1.Namespace {
namespace := &corev1.Namespace{
return &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "Namespace",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{
zarfManagedByLabel: "zarf",
},
},
}

// Merge in common labels so that later modifications to the namespace can't mutate them
namespace.ObjectMeta.Labels = helpers.MergeMap[string](k.Labels, namespace.ObjectMeta.Labels)

return namespace
}

// IsInitialNamespace returns true if the given namespace name is an initial k8s namespace: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/#initial-namespaces
Expand Down
5 changes: 1 addition & 4 deletions src/pkg/k8s/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"sort"
"time"

"github.com/defenseunicorns/pkg/helpers"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -27,12 +26,10 @@ func (k *K8s) GeneratePod(name, namespace string) *corev1.Pod {
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: make(Labels),
},
}

// Merge in common labels so that later modifications to the pod can't mutate them
pod.ObjectMeta.Labels = helpers.MergeMap[string](k.Labels, pod.ObjectMeta.Labels)

return pod
}

Expand Down
11 changes: 4 additions & 7 deletions src/pkg/k8s/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"crypto/tls"
"fmt"

"github.com/defenseunicorns/pkg/helpers"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -28,23 +27,21 @@ func (k *K8s) GetSecretsWithLabel(namespace, labelSelector string) (*corev1.Secr

// GenerateSecret returns a Kubernetes secret object without applying it to the cluster.
func (k *K8s) GenerateSecret(namespace, name string, secretType corev1.SecretType) *corev1.Secret {
secret := &corev1.Secret{
return &corev1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
zarfManagedByLabel: "zarf",
},
},
Type: secretType,
Data: map[string][]byte{},
}

// Merge in common labels so that later modifications to the secret can't mutate them
secret.ObjectMeta.Labels = helpers.MergeMap[string](k.Labels, secret.ObjectMeta.Labels)

return secret
}

// GenerateTLSSecret returns a Kubernetes secret object without applying it to the cluster.
Expand Down
4 changes: 1 addition & 3 deletions src/pkg/k8s/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ func (k *K8s) GenerateService(namespace, name string) *corev1.Service {
Name: name,
Namespace: namespace,
Annotations: make(Labels),
Labels: make(Labels),
},
}

// Merge in common labels so that later modifications to the service can't mutate them
service.ObjectMeta.Labels = helpers.MergeMap[string](k.Labels, service.ObjectMeta.Labels)

return service
}

Expand Down
Loading
Loading