-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kie-kogito-serverless-operator-405: Add external built image integri…
…ty validation
- Loading branch information
1 parent
cd39bac
commit f6214ed
Showing
19 changed files
with
304 additions
and
50 deletions.
There are no files selected for viewing
50 changes: 35 additions & 15 deletions
50
bundle/manifests/sonataflow-operator-builder-config_v1_configmap.yaml
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 |
---|---|---|
@@ -1,21 +1,41 @@ | ||
apiVersion: v1 | ||
data: | ||
DEFAULT_WORKFLOW_EXTENSION: .sw.json | ||
Dockerfile: "FROM docker.io/apache/incubator-kie-sonataflow-builder:main AS builder\n\n# | ||
variables that can be overridden by the builder\n# To add a Quarkus extension | ||
to your application\nARG QUARKUS_EXTENSIONS\n# Args to pass to the Quarkus CLI | ||
add extension command\nARG QUARKUS_ADD_EXTENSION_ARGS\n# Additional java/mvn arguments | ||
to pass to the builder\nARG MAVEN_ARGS_APPEND\n\n# Copy from build context to | ||
skeleton resources project\nCOPY --chown=1001 . ./resources\n\nRUN /home/kogito/launch/build-app.sh | ||
./resources\n \n#=============================\n# Runtime Run\n#=============================\nFROM | ||
registry.access.redhat.com/ubi9/openjdk-17-runtime:latest\n\nENV LANG='en_US.UTF-8' | ||
LANGUAGE='en_US:en'\n \n# We make four distinct layers so if there are application | ||
changes the library layers can be re-used\nCOPY --from=builder --chown=185 /home/kogito/serverless-workflow-project/target/quarkus-app/lib/ | ||
/deployments/lib/\nCOPY --from=builder --chown=185 /home/kogito/serverless-workflow-project/target/quarkus-app/*.jar | ||
/deployments/\nCOPY --from=builder --chown=185 /home/kogito/serverless-workflow-project/target/quarkus-app/app/ | ||
/deployments/app/\nCOPY --from=builder --chown=185 /home/kogito/serverless-workflow-project/target/quarkus-app/quarkus/ | ||
/deployments/quarkus/\n\nEXPOSE 8080\nUSER 185\nENV AB_JOLOKIA_OFF=\"\"\nENV JAVA_OPTS=\"-Dquarkus.http.host=0.0.0.0 | ||
-Djava.util.logging.manager=org.jboss.logmanager.LogManager\"\nENV JAVA_APP_JAR=\"/deployments/quarkus-run.jar\"\n" | ||
Dockerfile: | | ||
FROM docker.io/apache/incubator-kie-sonataflow-builder:main AS builder | ||
# variables that can be overridden by the builder | ||
# To add a Quarkus extension to your application | ||
ARG QUARKUS_EXTENSIONS | ||
# Args to pass to the Quarkus CLI add extension command | ||
ARG QUARKUS_ADD_EXTENSION_ARGS | ||
# Additional java/mvn arguments to pass to the builder | ||
ARG MAVEN_ARGS_APPEND | ||
# Copy from build context to skeleton resources project | ||
COPY --chown=1001 . ./resources | ||
RUN /home/kogito/launch/build-app.sh ./resources | ||
#============================= | ||
# Runtime Run | ||
#============================= | ||
FROM registry.access.redhat.com/ubi9/openjdk-17-runtime:latest | ||
ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' | ||
# We make four distinct layers so if there are application changes the library layers can be re-used | ||
COPY --from=builder --chown=185 /home/kogito/serverless-workflow-project/target/quarkus-app/lib/ /deployments/lib/ | ||
COPY --from=builder --chown=185 /home/kogito/serverless-workflow-project/target/quarkus-app/*.jar /deployments/ | ||
COPY --from=builder --chown=185 /home/kogito/serverless-workflow-project/target/quarkus-app/app/ /deployments/app/ | ||
COPY --from=builder --chown=185 /home/kogito/serverless-workflow-project/target/quarkus-app/quarkus/ /deployments/quarkus/ | ||
COPY --from=builder --chown=185 /home/kogito/serverless-workflow-project/target/classes/workflow.sw.json /deployments/app/workflow.sw.json | ||
EXPOSE 8080 | ||
USER 185 | ||
ENV AB_JOLOKIA_OFF="" | ||
ENV JAVA_OPTS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager" | ||
ENV JAVA_APP_JAR="/deployments/quarkus-run.jar" | ||
kind: ConfigMap | ||
metadata: | ||
name: sonataflow-operator-builder-config |
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
Binary file not shown.
Binary file not shown.
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
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,123 @@ | ||
// Copyright 2024 Apache Software Foundation (ASF) | ||
// | ||
// Licensed 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 validation | ||
|
||
import ( | ||
"archive/tar" | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
operatorapi "github.com/apache/incubator-kie-kogito-serverless-operator/api/v1alpha08" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-containerregistry/pkg/name" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/remote" | ||
"k8s.io/apimachinery/pkg/util/yaml" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type ImageValidator struct{} | ||
|
||
var workflowName = "deployments/app/workflow.sw.json" | ||
|
||
func (v ImageValidator) Validate(ctx context.Context, client client.Client, sonataflow *operatorapi.SonataFlow, req ctrl.Request) error { | ||
if sonataflow.HasContainerSpecImage() { | ||
err := client.Get(ctx, req.NamespacedName, sonataflow) | ||
equals, err := validateImage(sonataflow, sonataflow.Spec.PodTemplate.Container.Image) | ||
if err != nil { | ||
return err | ||
} | ||
if !equals { | ||
return fmt.Errorf("Workflow, defined in the image %s doesn't match deployment workflow", sonataflow.Spec.PodTemplate.Container.Image) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func validateImage(sonataflow *operatorapi.SonataFlow, image string) (bool, error) { | ||
imageRef, err := name.ParseReference(image) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
ref, err := remote.Image(imageRef) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
reader, err := readLayers(ref, workflowName) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
workflowDockerImage, err := jsonFromDockerImage(reader) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return cmp.Equal(workflowDockerImage, sonataflow.Spec.Flow), nil | ||
} | ||
|
||
func readLayers(image v1.Image, workflow string) (*tar.Reader, error) { | ||
layers, err := image.Layers() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for i := len(layers) - 1; i >= 0; i-- { | ||
if reader, err := readLayer(layers[i], workflow); err == nil && reader != nil { | ||
return reader, nil | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return nil, fmt.Errorf("file not found %s in docker image", workflow) | ||
} | ||
|
||
func readLayer(layer v1.Layer, workflow string) (*tar.Reader, error) { | ||
uncompressedLayer, err := layer.Uncompressed() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get uncompressed layer: %v", err) | ||
} | ||
defer uncompressedLayer.Close() | ||
|
||
tarReader := tar.NewReader(uncompressedLayer) | ||
for { | ||
header, err := tarReader.Next() | ||
if err != nil { | ||
if err.Error() == "EOF" { | ||
break | ||
} | ||
return nil, fmt.Errorf("error reading tar: %v", err) | ||
} | ||
|
||
if header.Typeflag == '0' && header.Name == workflow { | ||
return tarReader, nil | ||
} | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func jsonFromDockerImage(reader io.Reader) (operatorapi.Flow, error) { | ||
data, err := io.ReadAll(reader) | ||
workflow := &operatorapi.Flow{} | ||
if err = yaml.Unmarshal(data, workflow); err != nil { | ||
return *workflow, err | ||
} | ||
return *workflow, nil | ||
} |
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,40 @@ | ||
// Copyright 2024 Apache Software Foundation (ASF) | ||
// | ||
// Licensed 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 validation | ||
|
||
import ( | ||
"context" | ||
|
||
operatorapi "github.com/apache/incubator-kie-kogito-serverless-operator/api/v1alpha08" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
var validators = []Validator{ImageValidator{}} | ||
|
||
type Validator interface { | ||
Validate(ctx context.Context, client client.Client, sonataflow *operatorapi.SonataFlow, req ctrl.Request) error | ||
} | ||
|
||
// validate the SonataFlow object, right now it's only check if workflow is in deployment has image declared as that image | ||
// is the same as the image in the SonataFlow object | ||
func Validate(ctx context.Context, client client.Client, sonataflow *operatorapi.SonataFlow, req ctrl.Request) error { | ||
for _, validator := range validators { | ||
if err := validator.Validate(ctx, client, sonataflow, req); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
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.