Skip to content

Commit

Permalink
refactor: remove use of k8s dynamic (#2561)
Browse files Browse the repository at this point in the history
## Description

Removes use of k8s dynamic functions.

## Related Issue

Relates to #2507 

## Checklist before merging

- [x] Test, docs, adr added or updated as needed
- [x] [Contributor Guide
Steps](https://github.com/defenseunicorns/zarf/blob/main/.github/CONTRIBUTING.md#developer-workflow)
followed

Co-authored-by: Lucas Rodriguez <lucas.rodriguez@defenseunicorns.com>
  • Loading branch information
phillebaba and Lucas Rodriguez authored Jun 3, 2024
1 parent 90f038e commit 1749dd5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 105 deletions.
40 changes: 27 additions & 13 deletions src/internal/packager/git/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import (
"encoding/json"
"fmt"
"io"
netHttp "net/http"
"os"
"time"

netHttp "net/http"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/pkg/cluster"
"github.com/defenseunicorns/zarf/src/pkg/k8s"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/types"
"k8s.io/apimachinery/pkg/runtime/schema"
)

// CreateTokenResponse is the response given from creating a token in Gitea
Expand Down Expand Up @@ -253,24 +253,38 @@ func UpdateGiteaPVC(ctx context.Context, shouldRollBack bool) (string, error) {
}

pvcName := os.Getenv("ZARF_VAR_GIT_SERVER_EXISTING_PVC")
groupKind := schema.GroupKind{
Group: "",
Kind: "PersistentVolumeClaim",
}
labels := map[string]string{"app.kubernetes.io/managed-by": "Helm"}
annotations := map[string]string{"meta.helm.sh/release-name": "zarf-gitea", "meta.helm.sh/release-namespace": "zarf"}

if shouldRollBack {
err = c.K8s.RemoveLabelsAndAnnotations(ctx, cluster.ZarfNamespaceName, pvcName, groupKind, labels, annotations)
return "false", err
pvc, err := c.Clientset.CoreV1().PersistentVolumeClaims(cluster.ZarfNamespaceName).Get(ctx, pvcName, metav1.GetOptions{})
if err != nil {
return "false", err
}
delete(pvc.Labels, "app.kubernetes.io/managed-by")
delete(pvc.Annotations, "meta.helm.sh/release-name")
delete(pvc.Annotations, "meta.helm.sh/release-namespace")
_, err = c.Clientset.CoreV1().PersistentVolumeClaims(cluster.ZarfNamespaceName).Update(ctx, pvc, metav1.UpdateOptions{})
if err != nil {
return "false", err
}
return "false", nil
}

if pvcName == "data-zarf-gitea-0" {
err = c.K8s.AddLabelsAndAnnotations(ctx, cluster.ZarfNamespaceName, pvcName, groupKind, labels, annotations)
return "true", err
pvc, err := c.Clientset.CoreV1().PersistentVolumeClaims(cluster.ZarfNamespaceName).Get(ctx, pvcName, metav1.GetOptions{})
if err != nil {
return "true", err
}
pvc.Labels["app.kubernetes.io/managed-by"] = "Helm"
pvc.Annotations["meta.helm.sh/release-name"] = "zarf-gitea"
pvc.Annotations["meta.helm.sh/release-namespace"] = "zarf"
_, err = c.Clientset.CoreV1().PersistentVolumeClaims(cluster.ZarfNamespaceName).Update(ctx, pvc, metav1.UpdateOptions{})
if err != nil {
return "true", err
}
return "true", nil
}

return "false", err
return "false", nil
}

// DoHTTPThings adds http request boilerplate and perform the request, checking for a successful response.
Expand Down
56 changes: 47 additions & 9 deletions src/internal/packager/helm/post-render.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/defenseunicorns/zarf/src/types"
"helm.sh/helm/v3/pkg/releaseutil"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/restmapper"
"sigs.k8s.io/yaml"

kerrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -174,6 +176,16 @@ func (r *renderer) adoptAndUpdateNamespaces(ctx context.Context) error {
}

func (r *renderer) editHelmResources(ctx context.Context, resources []releaseutil.Manifest, finalManifestsOutput *bytes.Buffer) error {
dc, err := dynamic.NewForConfig(r.cluster.RestConfig)
if err != nil {
return err
}
groupResources, err := restmapper.GetAPIGroupResources(r.cluster.Clientset.Discovery())
if err != nil {
return err
}
mapper := restmapper.NewDiscoveryRESTMapper(groupResources)

for _, resource := range resources {
// parse to unstructured to have access to more data than just the name
rawData := &unstructured.Unstructured{}
Expand All @@ -199,8 +211,13 @@ func (r *renderer) editHelmResources(ctx context.Context, resources []releaseuti
case "Service":
// Check service resources for the zarf-connect label
labels := rawData.GetLabels()
if labels == nil {
labels = map[string]string{}
}
annotations := rawData.GetAnnotations()

if annotations == nil {
annotations = map[string]string{}
}
if key, keyExists := labels[config.ZarfConnectLabelName]; keyExists {
// If there is a zarf-connect label
message.Debugf("Match helm service %s for zarf connection %s", rawData.GetName(), key)
Expand All @@ -226,14 +243,35 @@ func (r *renderer) editHelmResources(ctx context.Context, resources []releaseuti
deployedNamespace = r.chart.Namespace
}

helmLabels := map[string]string{"app.kubernetes.io/managed-by": "Helm"}
helmAnnotations := map[string]string{
"meta.helm.sh/release-name": r.chart.ReleaseName,
"meta.helm.sh/release-namespace": r.chart.Namespace,
}

if err := r.cluster.AddLabelsAndAnnotations(ctx, deployedNamespace, rawData.GetName(), rawData.GroupVersionKind().GroupKind(), helmLabels, helmAnnotations); err != nil {
// Print a debug message since this could just be because the resource doesn't exist
err := func() error {
mapping, err := mapper.RESTMapping(rawData.GroupVersionKind().GroupKind())
if err != nil {
return err
}
resource, err := dc.Resource(mapping.Resource).Namespace(deployedNamespace).Get(ctx, rawData.GetName(), metav1.GetOptions{})
if err != nil {
return err
}
labels := resource.GetLabels()
if labels == nil {
labels = map[string]string{}
}
labels["app.kubernetes.io/managed-by"] = "Helm"
resource.SetLabels(labels)
annotations := resource.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
annotations["meta.helm.sh/release-name"] = r.chart.ReleaseName
annotations["meta.helm.sh/release-namespace"] = r.chart.Namespace
resource.SetAnnotations(annotations)
_, err = dc.Resource(mapping.Resource).Namespace(deployedNamespace).Update(ctx, resource, metav1.UpdateOptions{})
if err != nil {
return err
}
return nil
}()
if err != nil {
message.Debugf("Unable to adopt resource %s: %s", rawData.GetName(), err.Error())
}
}
Expand Down
83 changes: 0 additions & 83 deletions src/pkg/k8s/dynamic.go

This file was deleted.

0 comments on commit 1749dd5

Please sign in to comment.