-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-27613] Add label for the session job to help list the session …
…cluster
- Loading branch information
Aitozi
committed
Jun 14, 2022
1 parent
42f289a
commit bf60428
Showing
9 changed files
with
286 additions
and
10 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
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
94 changes: 94 additions & 0 deletions
94
...in/java/org/apache/flink/kubernetes/operator/admission/mutator/DefaultRequestMutator.java
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,94 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.flink.kubernetes.operator.admission.mutator; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.fabric8.kubernetes.api.model.KubernetesResource; | ||
import io.fabric8.kubernetes.api.model.admission.v1.AdmissionRequest; | ||
import io.fabric8.kubernetes.api.model.admission.v1.AdmissionResponse; | ||
import io.fabric8.zjsonpatch.JsonDiff; | ||
import io.javaoperatorsdk.admissioncontroller.AdmissionUtils; | ||
import io.javaoperatorsdk.admissioncontroller.NotAllowedException; | ||
import io.javaoperatorsdk.admissioncontroller.Operation; | ||
import io.javaoperatorsdk.admissioncontroller.RequestHandler; | ||
import io.javaoperatorsdk.admissioncontroller.clone.Cloner; | ||
import io.javaoperatorsdk.admissioncontroller.clone.ObjectMapperCloner; | ||
import io.javaoperatorsdk.admissioncontroller.mutation.Mutator; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
/** | ||
* The default request mutator. It's copied from the {@link | ||
* io.javaoperatorsdk.admissioncontroller.mutation.DefaultRequestMutator} with a modified path diff | ||
* util to serialize out include non-null. | ||
* | ||
* @param <T> | ||
*/ | ||
public class DefaultRequestMutator<T extends KubernetesResource> implements RequestHandler { | ||
private static final ObjectMapper mapper = | ||
new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
private final Mutator<T> mutator; | ||
private final Cloner<T> cloner; | ||
|
||
public DefaultRequestMutator(Mutator<T> mutator) { | ||
this(mutator, new ObjectMapperCloner<>()); | ||
} | ||
|
||
public DefaultRequestMutator(Mutator<T> mutator, Cloner<T> cloner) { | ||
this.mutator = mutator; | ||
this.cloner = cloner; | ||
} | ||
|
||
public AdmissionResponse handle(AdmissionRequest admissionRequest) { | ||
Operation operation = Operation.valueOf(admissionRequest.getOperation()); | ||
KubernetesResource originalResource = | ||
AdmissionUtils.getTargetResource(admissionRequest, operation); | ||
KubernetesResource clonedResource = | ||
(KubernetesResource) this.cloner.clone((T) originalResource); | ||
|
||
AdmissionResponse admissionResponse; | ||
try { | ||
T mutatedResource = this.mutator.mutate((T) clonedResource, operation); | ||
admissionResponse = admissionResponseFromMutation(originalResource, mutatedResource); | ||
} catch (NotAllowedException e) { | ||
admissionResponse = AdmissionUtils.notAllowedExceptionToAdmissionResponse(e); | ||
} | ||
|
||
return admissionResponse; | ||
} | ||
|
||
public static AdmissionResponse admissionResponseFromMutation( | ||
KubernetesResource originalResource, KubernetesResource mutatedResource) { | ||
AdmissionResponse admissionResponse = new AdmissionResponse(); | ||
admissionResponse.setAllowed(true); | ||
// It only allowed JSONPatch now, So we should avoid serialize out null value | ||
// https://github.com/kubernetes/kubernetes/blob/3f1a9f9f3eaeae3d387b9152ea9aebb52be72319/pkg/apis/admission/types.go#L134 | ||
admissionResponse.setPatchType("JSONPatch"); | ||
JsonNode originalResNode = mapper.valueToTree(originalResource); | ||
JsonNode mutatedResNode = mapper.valueToTree(mutatedResource); | ||
JsonNode diff = JsonDiff.asJson(originalResNode, mutatedResNode); | ||
String base64Diff = | ||
Base64.getEncoder() | ||
.encodeToString(diff.toString().getBytes(StandardCharsets.UTF_8)); | ||
admissionResponse.setPatch(base64Diff); | ||
return admissionResponse; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...ok/src/main/java/org/apache/flink/kubernetes/operator/admission/mutator/FlinkMutator.java
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,71 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.flink.kubernetes.operator.admission.mutator; | ||
|
||
import org.apache.flink.kubernetes.operator.crd.CrdConstants; | ||
import org.apache.flink.kubernetes.operator.crd.FlinkSessionJob; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.javaoperatorsdk.admissioncontroller.NotAllowedException; | ||
import io.javaoperatorsdk.admissioncontroller.Operation; | ||
import io.javaoperatorsdk.admissioncontroller.mutation.Mutator; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.HashMap; | ||
|
||
/** The default mutator. */ | ||
public class FlinkMutator implements Mutator<HasMetadata> { | ||
private static final Logger LOG = LoggerFactory.getLogger(FlinkMutator.class); | ||
private static final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
@Override | ||
public HasMetadata mutate(HasMetadata resource, Operation operation) | ||
throws NotAllowedException { | ||
if (operation == Operation.CREATE) { | ||
LOG.debug("Mutating resource {}", resource); | ||
|
||
if (CrdConstants.KIND_SESSION_JOB.equals(resource.getKind())) { | ||
try { | ||
var sessionJob = mapper.convertValue(resource, FlinkSessionJob.class); | ||
setSessionTargetLabel(sessionJob); | ||
return sessionJob; | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
return resource; | ||
} | ||
|
||
private void setSessionTargetLabel(FlinkSessionJob flinkSessionJob) { | ||
var labels = flinkSessionJob.getMetadata().getLabels(); | ||
if (labels == null) { | ||
labels = new HashMap<>(); | ||
} | ||
var deploymentName = flinkSessionJob.getSpec().getDeploymentName(); | ||
if (deploymentName != null | ||
&& !deploymentName.equals(labels.get(CrdConstants.LABEL_TARGET_SESSION))) { | ||
labels.put( | ||
CrdConstants.LABEL_TARGET_SESSION, | ||
flinkSessionJob.getSpec().getDeploymentName()); | ||
flinkSessionJob.getMetadata().setLabels(labels); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.