-
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 8 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,19 +56,25 @@ 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, | ||||||
} | ||||||
|
||||||
s.logger.Infof("Incoming request: %+v", in) | ||||||
s.logger.Infof("Incoming request: kind, %+v, Name %+v, Namespace %+v", in.Kind, in.Name, in.Namespace) | ||||||
|
||||||
defer s.logger.Infof("Outgoing response: %+v", resp) | ||||||
|
||||||
if in.Operation != admissionv1.Create { | ||||||
resp.Allowed = true | ||||||
return resp | ||||||
} | ||||||
|
||||||
s.logger.Infof("Unmarshall in of kind %s", in.Kind.Kind) | ||||||
|
||||||
p, metaPtr, spec := s.unmarshal(in) | ||||||
if spec == nil { | ||||||
resp.Allowed = true | ||||||
|
@@ -77,13 +83,15 @@ func (s *admissionWebhookServer) Review(in *admissionv1.AdmissionRequest) *admis | |||||
annotation := metaPtr.Annotations[s.config.Annotation] | ||||||
|
||||||
if annotation != "" { | ||||||
s.logger.Infof("%v annotation is present ", s.config.Annotation) | ||||||
bytes, err := json.Marshal([]jsonpatch.JsonPatchOperation{ | ||||||
s.createInitContainerPatch(p, annotation, spec.InitContainers), | ||||||
s.createContainerPatch(p, annotation, spec.Containers), | ||||||
s.createVolumesPatch(p, spec.Volumes), | ||||||
s.createLabelPatch(p, metaPtr.Labels), | ||||||
}) | ||||||
if err != nil { | ||||||
s.logger.Info("Some error happened") | ||||||
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
|
||||||
resp.Result = &v1.Status{ | ||||||
Status: err.Error(), | ||||||
} | ||||||
|
@@ -95,36 +103,48 @@ func (s *admissionWebhookServer) Review(in *admissionv1.AdmissionRequest) *admis | |||||
} | ||||||
|
||||||
resp.Allowed = true | ||||||
s.logger.Infof("Response") | ||||||
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
|
||||||
return resp | ||||||
} | ||||||
|
||||||
func (s *admissionWebhookServer) unmarshal(in *admissionv1.AdmissionRequest) (p string, meta *v1.ObjectMeta, spec *corev1.PodSpec) { | ||||||
var podSpec *corev1.PodSpec | ||||||
var podMetaPtr *v1.ObjectMeta | ||||||
var metaPtr *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 | ||||||
default: | ||||||
return "", nil, nil | ||||||
} | ||||||
|
@@ -133,10 +153,28 @@ func (s *admissionWebhookServer) unmarshal(in *admissionv1.AdmissionRequest) (p | |||||
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) | ||||||
} | ||||||
// Annotations shouldn't be applied second time. | ||||||
if in.Kind.Kind == replicaSetKind { | ||||||
for _, o := range metaPtr.OwnerReferences { | ||||||
if o.Kind == deploymentKind { | ||||||
return "", 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. Can we move this into replicaSetKind case at line 137? 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. No, we can't because unmarshal to target is performed after switch. 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. Could we please use a simple defer? 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. Fix was added. |
||||||
return p, metaPtr, podSpec | ||||||
|
||||||
func() { | ||||||
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
|
||||||
if in.Kind.Kind != podKind && metaPtr.Annotations != nil { | ||||||
if podMetaPtr.Annotations == nil { | ||||||
podMetaPtr.Annotations = metaPtr.Annotations | ||||||
} | ||||||
s.logger.Errorf("Malformed specification. Annotations can't be provided in several places.") | ||||||
} | ||||||
}() | ||||||
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
|
||||||
|
||||||
return 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