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 Deployment and StatefulSet Name Collision #58

Merged
merged 4 commits into from
Jan 28, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
dist/
node_modules
7 changes: 5 additions & 2 deletions instrumentor/controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
var (
IgnoredNamespaces = []string{"kube-system", "local-path-storage", "istio-system", "linkerd", consts.DefaultNamespace}
SkipAnnotation = "odigos.io/skip"

DeploymentPrefix = "deployment-"
StatefulSetPrefix = "statefulset-"
)

func shouldSkip(annotations map[string]string, namespace string) bool {
Expand All @@ -38,7 +41,7 @@ func shouldSkip(annotations map[string]string, namespace string) bool {
}

func syncInstrumentedApps(ctx context.Context, req *ctrl.Request, c client.Client, scheme *runtime.Scheme,
readyReplicas int32, object client.Object, podTemplateSpec *v1.PodTemplateSpec, ownerKey string) error {
readyReplicas int32, object client.Object, podTemplateSpec *v1.PodTemplateSpec, ownerKey string, prefix string) error {

logger := log.FromContext(ctx)
instApps, err := getInstrumentedApps(ctx, req, c, ownerKey)
Expand All @@ -55,7 +58,7 @@ func syncInstrumentedApps(ctx context.Context, req *ctrl.Request, c client.Clien

instrumentedApp := odigosv1.InstrumentedApplication{
ObjectMeta: metav1.ObjectMeta{
Name: req.Name,
Name: prefix + req.Name,
Namespace: req.Namespace,
},
Spec: odigosv1.InstrumentedApplicationSpec{
Expand Down
3 changes: 2 additions & 1 deletion instrumentor/controllers/deployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ func (r *DeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, nil
}

err = syncInstrumentedApps(ctx, &req, r.Client, r.Scheme, dep.Status.ReadyReplicas, &dep, &dep.Spec.Template, instAppDepOwnerKey)
err = syncInstrumentedApps(ctx, &req, r.Client, r.Scheme, dep.Status.ReadyReplicas,
&dep, &dep.Spec.Template, instAppDepOwnerKey, DeploymentPrefix)
if err != nil {
logger.Error(err, "error syncing instrumented apps with deployments")
return ctrl.Result{}, err
Expand Down
3 changes: 2 additions & 1 deletion instrumentor/controllers/statefulset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func (r *StatefulSetReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, nil
}

err = syncInstrumentedApps(ctx, &req, r.Client, r.Scheme, ss.Status.ReadyReplicas, &ss, &ss.Spec.Template, instAppSSOwnerKey)
err = syncInstrumentedApps(ctx, &req, r.Client, r.Scheme, ss.Status.ReadyReplicas,
&ss, &ss.Spec.Template, instAppSSOwnerKey, StatefulSetPrefix)
if err != nil {
logger.Error(err, "error syncing instrumented apps with statefulsets")
return ctrl.Result{}, err
Expand Down
4 changes: 3 additions & 1 deletion ui/pages/api/apps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { NextApiRequest, NextApiResponse } from "next";
import * as k8s from "@kubernetes/client-node";
import type { AppsApiResponse, ApplicationData } from "@/types/apps";
import {stripPrefix} from "@/utils/crd";

type Error = {
message: string;
Expand Down Expand Up @@ -40,13 +41,14 @@ export default async function handler(

return {
id: item.metadata.uid,
name: item.metadata.ownerReferences[0].name,
name: stripPrefix(item.metadata.ownerReferences[0].name),
languages: languages,
instrumented: item.status.instrumented,
kind: item.metadata.ownerReferences[0].kind,
namespace: item.metadata.namespace,
};
});

const discoveryInProgress = response.body.items.some(
(i: any) => i.status.langDetection.phase === "Running"
);
Expand Down
13 changes: 13 additions & 0 deletions ui/utils/crd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const DEPLOYMENT_PREFIX = "deployment-";
const STATEFUL_SET_PREFIX = "statefulset-";

// stripPrefix removes StatefulSet- and Deployment- prefixes
export function stripPrefix(name:string):string {
if (name.startsWith(DEPLOYMENT_PREFIX)){
return name.substring(DEPLOYMENT_PREFIX.length)
} else if (name.startsWith(STATEFUL_SET_PREFIX)){
return name.substring(STATEFUL_SET_PREFIX.length)
}

return name
}