MCPcopy Index your code
hub / github.com/devspace-sh/devspace / findTargetBySelector

Function findTargetBySelector

pkg/devspace/services/podreplace/find.go:43–107  ·  view source on GitHub ↗
(ctx devspacecontext.Context, devPod *latest.DevPod, filter func(obj metav1.Object) bool)

Source from the content-addressed store, hash-verified

41}
42
43func findTargetBySelector(ctx devspacecontext.Context, devPod *latest.DevPod, filter func(obj metav1.Object) bool) (runtime.Object, error) {
44 namespace := ctx.KubeClient().Namespace()
45 if devPod.Namespace != "" {
46 namespace = devPod.Namespace
47 }
48
49 // deployments
50 deployments, err := ctx.KubeClient().KubeClient().AppsV1().Deployments(namespace).List(ctx.Context(), metav1.ListOptions{})
51 if err != nil {
52 return nil, errors.Wrap(err, "list Deployments")
53 }
54 for _, d := range deployments.Items {
55 if filter != nil && !filter(&d) {
56 continue
57 }
58
59 matched, err := matchesSelector(ctx, &d.Spec.Template, devPod)
60 if err != nil {
61 return nil, err
62 } else if matched {
63 d.Kind = "Deployment"
64 return &d, nil
65 }
66 }
67
68 // replicaSets
69 replicaSets, err := ctx.KubeClient().KubeClient().AppsV1().ReplicaSets(namespace).List(ctx.Context(), metav1.ListOptions{})
70 if err != nil {
71 return nil, errors.Wrap(err, "list ReplicaSets")
72 }
73 for _, d := range replicaSets.Items {
74 if len(d.OwnerReferences) > 0 || (filter != nil && !filter(&d)) {
75 continue
76 }
77
78 matched, err := matchesSelector(ctx, &d.Spec.Template, devPod)
79 if err != nil {
80 return nil, err
81 } else if matched {
82 d.Kind = "ReplicaSet"
83 return &d, nil
84 }
85 }
86
87 // statefulSets
88 statefulSets, err := ctx.KubeClient().KubeClient().AppsV1().StatefulSets(namespace).List(ctx.Context(), metav1.ListOptions{})
89 if err != nil {
90 return nil, errors.Wrap(err, "list StatefulSets")
91 }
92 for _, d := range statefulSets.Items {
93 if filter != nil && !filter(&d) {
94 continue
95 }
96
97 matched, err := matchesSelector(ctx, &d.Spec.Template, devPod)
98 if err != nil {
99 return nil, err
100 } else if matched {

Callers 1

ReplacePodMethod · 0.85

Calls 6

filterStruct · 0.85
matchesSelectorFunction · 0.85
NamespaceMethod · 0.65
KubeClientMethod · 0.65
ListMethod · 0.65
ContextMethod · 0.65

Tested by

no test coverage detected