This repository was archived by the owner on Jan 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding the
kardinal manager deploy
and `kardinal manager remo…
…ve` CLI commands (#10)
- Loading branch information
Showing
9 changed files
with
443 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package consts | ||
|
||
const ( | ||
KardinalAppIDLabelKey = "dev.kardinal.app-id" | ||
KardinalManagerAppIDLabelValue = "kardinal-manager" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package deployment | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"github.com/kurtosis-tech/stacktrace" | ||
"kardinal.cli/consts" | ||
"text/template" | ||
) | ||
|
||
const ( | ||
kardinalNamespace = "default" | ||
kardinalManagerDeploymentTmplName = "kardinal-manager-deployment" | ||
|
||
kardinalManagerDeploymentTmpl = ` | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: kardinal-manager | ||
namespace: {{.Namespace}} | ||
labels: | ||
{{.KardinalAppIDLabelKey}}: {{.KardinalManagerAppIDLabelValue}} | ||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
name: kardinal-manager-role | ||
labels: | ||
{{.KardinalAppIDLabelKey}}: {{.KardinalManagerAppIDLabelValue}} | ||
rules: | ||
- apiGroups: ["*"] | ||
resources: ["namespaces", "pods", "services", "deployments", "virtualservices", "workloadgroups", "workloadentries", "sidecars", "serviceentries", "gateways", "envoyfilters", "destinationrules"] | ||
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"] | ||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: kardinal-manager-binding | ||
labels: | ||
{{.KardinalAppIDLabelKey}}: {{.KardinalManagerAppIDLabelValue}} | ||
subjects: | ||
- kind: ServiceAccount | ||
name: kardinal-manager | ||
namespace: default | ||
roleRef: | ||
kind: ClusterRole | ||
name: kardinal-manager-role | ||
apiGroup: rbac.authorization.k8s.io | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: kardinal-manager | ||
namespace: {{.Namespace}} | ||
labels: | ||
{{.KardinalAppIDLabelKey}}: {{.KardinalManagerAppIDLabelValue}} | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
{{.KardinalAppIDLabelKey}}: {{.KardinalManagerAppIDLabelValue}} | ||
template: | ||
metadata: | ||
labels: | ||
{{.KardinalAppIDLabelKey}}: {{.KardinalManagerAppIDLabelValue}} | ||
spec: | ||
serviceAccountName: kardinal-manager | ||
containers: | ||
- name: kardinal-manager | ||
image: kurtosistech/kardinal-manager:latest | ||
# TODO: Policy to local dev only - figure a way to remove it | ||
imagePullPolicy: Never | ||
env: | ||
- name: KUBERNETES_SERVICE_HOST | ||
value: "kubernetes.default.svc" | ||
- name: KUBERNETES_SERVICE_PORT | ||
value: "443" | ||
- name: KARDINAL_MANAGER_CLUSTER_CONFIG_ENDPOINT | ||
value: "{{.ClusterResourcesURL}}" | ||
- name: KARDINAL_MANAGER_FETCHER_JOB_DURATION_SECONDS | ||
value: "10" | ||
` | ||
) | ||
|
||
type templateData struct { | ||
Namespace string | ||
ClusterResourcesURL string | ||
KardinalAppIDLabelKey string | ||
KardinalManagerAppIDLabelValue string | ||
} | ||
|
||
func DeployKardinalManagerInCluster(ctx context.Context, clusterResourcesURL string) error { | ||
kubernetesClientObj, err := createKubernetesClient() | ||
if err != nil { | ||
return stacktrace.Propagate(err, "An error occurred while creating the Kubernetes client") | ||
} | ||
|
||
kardinalManagerDeploymentTemplate, err := template.New(kardinalManagerDeploymentTmplName).Parse(kardinalManagerDeploymentTmpl) | ||
if err != nil { | ||
return stacktrace.Propagate(err, "An error occurred while parsing the kardinal-manager deployment template") | ||
} | ||
|
||
templateDataObj := templateData{ | ||
Namespace: kardinalNamespace, | ||
ClusterResourcesURL: clusterResourcesURL, | ||
KardinalAppIDLabelKey: consts.KardinalAppIDLabelKey, | ||
KardinalManagerAppIDLabelValue: consts.KardinalManagerAppIDLabelValue, | ||
} | ||
|
||
yamlFileContentsBuffer := &bytes.Buffer{} | ||
|
||
if err = kardinalManagerDeploymentTemplate.Execute(yamlFileContentsBuffer, templateDataObj); err != nil { | ||
return stacktrace.Propagate(err, "An error occurred while executing the template '%s' with data objects '%+v'", kardinalManagerDeploymentTmplName, templateDataObj) | ||
} | ||
|
||
if err = kubernetesClientObj.ApplyYamlFileContentInNamespace(ctx, kardinalNamespace, yamlFileContentsBuffer.Bytes()); err != nil { | ||
return stacktrace.Propagate(err, "An error occurred while applying the kardinal-manager deployment") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func RemoveKardinalManagerFromCluster(ctx context.Context) error { | ||
kubernetesClientObj, err := createKubernetesClient() | ||
if err != nil { | ||
return stacktrace.Propagate(err, "An error occurred while creating the Kubernetes client") | ||
} | ||
|
||
labels := map[string]string{ | ||
consts.KardinalAppIDLabelKey: consts.KardinalManagerAppIDLabelValue, | ||
} | ||
|
||
if err = kubernetesClientObj.RemoveNamespaceResourcesByLabels(ctx, kardinalNamespace, labels); err != nil { | ||
return stacktrace.Propagate(err, "An error occurred while removing the kardinal-manager from the cluster using labels '%+v'", labels) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.