Skip to content

Commit

Permalink
refactor: remove use of k8s dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
phillebaba committed May 30, 2024
1 parent c09cac5 commit 5c8ca8a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 104 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 "false", 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 "false", 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
40 changes: 32 additions & 8 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,13 @@ func (r *renderer) adoptAndUpdateNamespaces(ctx context.Context) error {
}

func (r *renderer) editHelmResources(ctx context.Context, resources []releaseutil.Manifest, finalManifestsOutput *bytes.Buffer) error {
dc := dynamic.New(r.cluster.Clientset.Discovery().RESTClient())
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 Down Expand Up @@ -226,14 +235,29 @@ 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()
labels["app.kubernetes.io/managed-by"] = "Helm"
resource.SetLabels(labels)
annotations := resource.GetAnnotations()
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 5c8ca8a

Please sign in to comment.