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

Merge Common vars insteaad of using the global #1992

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/pkg/k8s/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"fmt"

"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -28,11 +29,13 @@ func (k *K8s) CreateConfigmap(namespace, name string, data map[string][]byte) (*
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: k.Labels,
},
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: 8 additions & 3 deletions src/pkg/k8s/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"cuelang.org/go/pkg/strings"
"github.com/defenseunicorns/zarf/src/pkg/utils/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 @@ -63,16 +64,20 @@ 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 {
return &corev1.Namespace{
namespace := &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "Namespace",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: k.Labels,
Name: name,
},
}

// 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
9 changes: 7 additions & 2 deletions src/pkg/k8s/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sort"
"time"

"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -18,17 +19,21 @@ const waitLimit = 30

// GeneratePod creates a new pod without adding it to the k8s cluster.
func (k *K8s) GeneratePod(name, namespace string) *corev1.Pod {
return &corev1.Pod{
pod := &corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "Pod",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: k.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
}

// DeletePod removes a pod from the cluster by namespace & name.
Expand Down
9 changes: 7 additions & 2 deletions src/pkg/k8s/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package k8s
import (
"context"

"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand All @@ -22,7 +23,7 @@ func (k *K8s) ReplaceService(service *corev1.Service) (*corev1.Service, error) {

// GenerateService returns a K8s service struct without writing to the cluster.
func (k *K8s) GenerateService(namespace, name string) *corev1.Service {
return &corev1.Service{
service := &corev1.Service{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "Service",
Expand All @@ -31,9 +32,13 @@ func (k *K8s) GenerateService(namespace, name string) *corev1.Service {
Name: name,
Namespace: namespace,
Annotations: make(Labels),
Labels: k.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
}

// DeleteService removes a service from the cluster by namespace and name.
Expand Down