-
Notifications
You must be signed in to change notification settings - Fork 16
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
Add support for ReplicaSet k8s kind #152
Changes from 12 commits
8939c58
c9ef8c5
2f22dca
0c5016d
ce2d6a3
9c8733a
3131c88
ddf76f5
2dccc62
951d0e4
ead9483
d7709e4
d3b2f29
c5c4f45
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -56,6 +56,12 @@ type admissionWebhookServer struct { | |||||
logger *zap.SugaredLogger | ||||||
} | ||||||
|
||||||
const ( | ||||||
deploymentKind string = "Deployment" | ||||||
podKind string = "Pod" | ||||||
replicaSetKind string = "ReplicaSet" | ||||||
) | ||||||
|
||||||
func (s *admissionWebhookServer) Review(in *admissionv1.AdmissionRequest) *admissionv1.AdmissionResponse { | ||||||
var resp = &admissionv1.AdmissionResponse{ | ||||||
UID: in.UID, | ||||||
|
@@ -100,43 +106,68 @@ func (s *admissionWebhookServer) Review(in *admissionv1.AdmissionRequest) *admis | |||||
|
||||||
func (s *admissionWebhookServer) unmarshal(in *admissionv1.AdmissionRequest) (p string, meta *v1.ObjectMeta, spec *corev1.PodSpec) { | ||||||
var podSpec *corev1.PodSpec | ||||||
var metaPtr *v1.ObjectMeta | ||||||
var metaPtr, podMetaPtr *v1.ObjectMeta | ||||||
var target interface{} | ||||||
p = "/spec/template" | ||||||
switch in.Kind.Kind { | ||||||
case "Deployment": | ||||||
case deploymentKind: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
var deployment appsv1.Deployment | ||||||
metaPtr = &deployment.Spec.Template.ObjectMeta | ||||||
metaPtr = &deployment.ObjectMeta | ||||||
podMetaPtr = &deployment.Spec.Template.ObjectMeta | ||||||
podSpec = &deployment.Spec.Template.Spec | ||||||
target = &deployment | ||||||
case "Pod": | ||||||
case podKind: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
var pod corev1.Pod | ||||||
p = "" | ||||||
metaPtr = &pod.ObjectMeta | ||||||
podMetaPtr = &pod.ObjectMeta | ||||||
podSpec = &pod.Spec | ||||||
target = &pod | ||||||
case "DaemonSet": | ||||||
var daemonSet appsv1.DaemonSet | ||||||
metaPtr = &daemonSet.Spec.Template.ObjectMeta | ||||||
metaPtr = &daemonSet.ObjectMeta | ||||||
podMetaPtr = &daemonSet.Spec.Template.ObjectMeta | ||||||
podSpec = &daemonSet.Spec.Template.Spec | ||||||
target = &daemonSet | ||||||
case "StatefulSet": | ||||||
var statefulSet appsv1.StatefulSet | ||||||
metaPtr = &statefulSet.Spec.Template.ObjectMeta | ||||||
metaPtr = &statefulSet.ObjectMeta | ||||||
podMetaPtr = &statefulSet.Spec.Template.ObjectMeta | ||||||
podSpec = &statefulSet.Spec.Template.Spec | ||||||
target = &statefulSet | ||||||
case replicaSetKind: | ||||||
var replicaSet appsv1.ReplicaSet | ||||||
metaPtr = &replicaSet.ObjectMeta | ||||||
podMetaPtr = &replicaSet.Spec.Template.ObjectMeta | ||||||
podSpec = &replicaSet.Spec.Template.Spec | ||||||
target = &replicaSet | ||||||
defer func() { | ||||||
s.logger.Info("Replicaset Defer method") | ||||||
for _, o := range metaPtr.OwnerReferences { | ||||||
if o.Kind == deploymentKind { | ||||||
p, meta, spec = "", nil, nil | ||||||
} | ||||||
} | ||||||
}() | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is still looking overcomplicated. I think the main problem here that we are doing things in Please move all 'post unmarshal' changes into separate method and use it right afiter unmarshal in the Review function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added changes, please take a look |
||||||
|
||||||
default: | ||||||
return "", nil, nil | ||||||
} | ||||||
|
||||||
if err := json.Unmarshal(in.Object.Raw, target); err != nil { | ||||||
return "", nil, nil | ||||||
} | ||||||
p = path.Join("/", p) | ||||||
if metaPtr.Labels == nil { | ||||||
metaPtr.Labels = make(map[string]string) | ||||||
if podMetaPtr.Labels == nil { | ||||||
podMetaPtr.Labels = make(map[string]string) | ||||||
} | ||||||
return p, metaPtr, podSpec | ||||||
// Annotations shouldn't be applied second time. | ||||||
if in.Kind.Kind != podKind && metaPtr.Annotations != nil { | ||||||
if podMetaPtr.Annotations == nil { | ||||||
podMetaPtr.Annotations = metaPtr.Annotations | ||||||
} else { | ||||||
s.logger.Errorf("Malformed specification. Annotations can't be provided in several places.") | ||||||
} | ||||||
} | ||||||
|
||||||
return path.Join("/", p), podMetaPtr, podSpec | ||||||
} | ||||||
|
||||||
func (s *admissionWebhookServer) createVolumesPatch(p string, volumes []corev1.Volume) jsonpatch.JsonPatchOperation { | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anastasia-malysheva Could you please resolve this comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@denis-tingaikin I took a look to the k8s doc and couldn't find constants for this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@denis-tingaikin I changed linter setting and removed constants