Skip to content
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

Rebuild changed components only #965

Merged
merged 24 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a8d49c6
update unit test for build enabled components
nilsgstrabo Oct 27, 2023
75a18e2
unfinished build container pipeline
nilsgstrabo Oct 31, 2023
2f44080
refactory build image and deploy pipline steps
nilsgstrabo Nov 1, 2023
fb02c2a
unit tests
nilsgstrabo Nov 2, 2023
99a6a66
Merge branch 'master' into rebuild-changed-components-only-2
nilsgstrabo Nov 2, 2023
8d86f37
fail deploy pipeline when contains components for build
nilsgstrabo Nov 3, 2023
3c2fd79
rebuild all components when buildsecret changed
nilsgstrabo Nov 3, 2023
fd49f22
refactor hashing of ra and build secret
nilsgstrabo Nov 3, 2023
70a1a58
refactor pipeline hash func
nilsgstrabo Nov 6, 2023
0062492
remove println
nilsgstrabo Nov 6, 2023
34bb785
update chart
nilsgstrabo Nov 6, 2023
a7b3dae
launch.json
nilsgstrabo Nov 6, 2023
0e20c81
handle env namespace or rolebinding not yet created
nilsgstrabo Nov 6, 2023
2e97bee
handle forbidden error for build secret
nilsgstrabo Nov 6, 2023
f82f093
split setBuildAndDeployImages
nilsgstrabo Nov 7, 2023
db621b6
refactored target environments in pipeline
nilsgstrabo Nov 7, 2023
4110cdb
reanme pipeline property
nilsgstrabo Nov 7, 2023
791f823
fix comment
nilsgstrabo Nov 7, 2023
520767c
simplify if statement
nilsgstrabo Nov 7, 2023
7a97999
fix typo
nilsgstrabo Nov 7, 2023
b52ad33
Merge branch 'master' into rebuild-changed-components-only-2
nilsgstrabo Nov 7, 2023
521e43a
use magic string for hash when build secret or ra nil
nilsgstrabo Nov 7, 2023
88bc550
log component image source decisions
nilsgstrabo Nov 7, 2023
96d3bd9
fix logging
nilsgstrabo Nov 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,24 @@
"env": {},
"args": [
"--RADIX_APP=radix-job-demo",
"--JOB_NAME=radix-pipeline-20230321154251-nntcf",
"--JOB_NAME=radix-pipeline-20231030143058-mtwyg",
"--PIPELINE_TYPE=build-deploy",
"--RADIX_TEKTON_IMAGE=radix-tekton:main-latest",
"--RADIX_IMAGE_BUILDER=radix-image-builder:master-latest",
"--RADIX_BUILDAH_IMAGE_BUILDER=quay.io/buildah/stable:v1.31",
"--SECCOMP_PROFILE_FILENAME=allow-buildah.json",
"--RADIX_CLUSTER_TYPE=development",
"--RADIX_ZONE=dev",
"--RADIX_CLUSTERNAME=weekly-12",
"--RADIX_CLUSTERNAME=weekly-44",
"--RADIX_CONTAINER_REGISTRY=radixdev.azurecr.io",
"--AZURE_SUBSCRIPTION_ID=16ede44b-1f74-40a5-b428-46cca9a5741b",
"--IMAGE_TAG=nntcf",
"--IMAGE_TAG=abcde",
"--BRANCH=main",
"--COMMIT_ID=2bd977cd31687ae97f00b5b7bf0fe44a7608611a",
"--COMMIT_ID=890e19c7bea84678d684daa2a9363cd8be4940bb",
"--PUSH_IMAGE=true",
"--USE_CACHE=true",
"--RADIX_FILE_NAME=/workspace/radixconfig.yaml",
"--TO_ENVIRONMENT=dev",
"--TO_ENVIRONMENT=qa",
"--IMAGE_TAG_NAME=server=1.23-alpine-slim",
"--IMAGE_TAG_NAME=server2=1.22.1-alpine-perl"
]
Expand All @@ -38,17 +40,39 @@
"program": "${workspaceFolder}/pipeline-runner/main.go",
"env": {},
"args": [
"--RADIX_APP=radix-github-webhook",
"--RADIX_APP=radix-job-demo",
"--IMAGE_TAG=abcdef",
"--JOB_NAME=radix-pipeline-promotion-1",
"--PIPELINE_TYPE=promote",
"--RADIX_TEKTON_IMAGE=radix-tekton:main-latest",
"--FROM_ENVIRONMENT=qa",
"--TO_ENVIRONMENT=prod",
"--DEPLOYMENT_NAME=qa-etxkt-ac6rxchq",
"--DEBUG=true",
"--RADIX_CONTAINER_REGISTRY=radixdev.azurecr.io"
]
},
{
"name": "Launch-deploy-pipeline",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/pipeline-runner/main.go",
"env": {},
"args": [
"--RADIX_APP=radix-job-demo",
"--JOB_NAME=radix-pipeline-20231030091802-mfzoz",
"--PIPELINE_TYPE=deploy",
"--RADIX_TEKTON_IMAGE=radix-tekton:main-latest",
"--TO_ENVIRONMENT=prod",
"--DEBUG=true",
"--RADIX_FILE_NAME=/workspace/radixconfig.yaml",
"--RADIX_CLUSTER_TYPE=development",
"--RADIX_ZONE=dev",
"--RADIX_CLUSTERNAME=weekly-44",
"--RADIX_CONTAINER_REGISTRY=radixdev.azurecr.io"
]
},
{
"name": "Launch-operator",
"type": "go",
Expand All @@ -73,7 +97,7 @@
"RADIXOPERATOR_APP_ROLLING_UPDATE_MAX_SURGE": "25%",
"RADIXOPERATOR_APP_READINESS_PROBE_INITIAL_DELAY_SECONDS": "5",
"RADIXOPERATOR_APP_READINESS_PROBE_PERIOD_SECONDS": "10",
"RADIX_ACTIVE_CLUSTERNAME": "weekly-xx",
"RADIX_ACTIVE_CLUSTERNAME": "weekly-45",
"RADIX_IMAGE_BUILDER": "radix-image-builder:master-latest",
"RADIX_TEKTON_IMAGE": "radix-tekton:main-latest",
"RADIXOPERATOR_JOB_SCHEDULER": "radix-job-scheduler:main-latest",
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ mocks:
mockgen -source ./pkg/apis/batch/syncer.go -destination ./pkg/apis/batch/syncer_mock.go -package batch
mockgen -source ./pkg/apis/batch/syncerfactory.go -destination ./pkg/apis/batch/syncerfactory_mock.go -package batch
mockgen -source ./radix-operator/common/handler.go -destination ./radix-operator/common/handler_mock.go -package common
mockgen -source ./pipeline-runner/wait/job.go -destination ./pipeline-runner/wait/job_mock.go -package wait
mockgen -source ./pipeline-runner/internal/wait/job.go -destination ./pipeline-runner/internal/wait/job_mock.go -package wait

.PHONY: build-pipeline
build-pipeline:
Expand Down
4 changes: 2 additions & 2 deletions charts/radix-operator/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: v2
name: radix-operator
version: 1.24.1
appVersion: 1.44.1
version: 1.25.0
appVersion: 1.45.0
kubeVersion: ">=1.24.0"
description: Radix Operator
keywords:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strings"
"testing"

"github.com/equinor/radix-operator/pipeline-runner/utils/commandbuilder"
"github.com/equinor/radix-operator/pipeline-runner/internal/commandbuilder"
"github.com/stretchr/testify/assert"
)

Expand Down
64 changes: 64 additions & 0 deletions pipeline-runner/internal/hash/encoding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package hash

import (
"encoding/binary"
"fmt"
"math"
"reflect"

yamlk8s "sigs.k8s.io/yaml"
)

type encoder func(v any) ([]byte, error)

func boolEncoder(v any) ([]byte, error) {
if reflect.ValueOf(v).Bool() {
return []byte{1}, nil
}
return []byte{0}, nil
}

func intEncoder(v any) ([]byte, error) {
vt := reflect.ValueOf(v)
return binary.AppendVarint([]byte{}, vt.Int()), nil
}

func uintEncoder(v any) ([]byte, error) {
vt := reflect.ValueOf(v)
return binary.AppendUvarint([]byte{}, vt.Uint()), nil
}

func floatEncoder(v any) ([]byte, error) {
vt := reflect.ValueOf(v)
return uintEncoder(math.Float64bits(vt.Float()))
}

func stringEncoder(v any) ([]byte, error) {
vt := reflect.ValueOf(v)
return []byte(vt.String()), nil
}

func structEncoder(v any) ([]byte, error) {
b, err := yamlk8s.Marshal(v)
return b, err
}

func getEncoder(v any) (encoder, error) {
t := reflect.Indirect(reflect.ValueOf(v))

switch t.Kind() {
case reflect.Bool:
return boolEncoder, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return intEncoder, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return uintEncoder, nil
case reflect.Float32, reflect.Float64:
return floatEncoder, nil
case reflect.String:
return stringEncoder, nil
case reflect.Struct, reflect.Map, reflect.Slice, reflect.Array:
return structEncoder, nil
}
return nil, fmt.Errorf("encoder for type %s not supported", t.Kind().String())
}
62 changes: 62 additions & 0 deletions pipeline-runner/internal/hash/hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package hash

import (
"encoding/hex"
"fmt"
"strings"
)

type Algorithm string

const (
hashStringSeparator string = "="
)

type sumFunc func(in []byte) []byte

var sumProviders map[Algorithm]sumFunc = map[Algorithm]sumFunc{}

func registerProvider(alg Algorithm, provider sumFunc) {
sumProviders[alg] = provider
}

func ToHashString(alg Algorithm, source any) (string, error) {
sum, found := sumProviders[alg]
if !found {
return "", fmt.Errorf("hash algorithm %s not found", alg)
}

encode, err := getEncoder(source)
if err != nil {
return "", err
}

b, err := encode(source)
if err != nil {
return "", err
}

hashBytes := sum(b)
return joinToHashString(alg, hex.EncodeToString(hashBytes)), nil
}

func CompareWithHashString(source any, targetHashString string) (bool, error) {
alg := extractAlgorithmFromHashString(targetHashString)
sourceHashString, err := ToHashString(alg, source)
if err != nil {
return false, err
}
return targetHashString == sourceHashString, nil
}

func joinToHashString(alg Algorithm, hash string) string {
return fmt.Sprintf("%s%s%s", alg, hashStringSeparator, hash)
}

func extractAlgorithmFromHashString(hashString string) Algorithm {
parts := strings.Split(hashString, hashStringSeparator)
if len(parts) != 2 {
return ""
}
return Algorithm(parts[0])
}
82 changes: 82 additions & 0 deletions pipeline-runner/internal/hash/hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package hash_test

import (
"encoding/hex"
"strings"
"testing"

"github.com/equinor/radix-operator/pipeline-runner/internal/hash"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_ToHashString(t *testing.T) {
type dataStruct struct {
S string
}

type algorithm struct {
alg hash.Algorithm
hashLen int
}
algorithms := []algorithm{{alg: hash.SHA256, hashLen: 32}}

type testSpec struct {
test string
val any
valOther any
match bool
}

tests := []testSpec{
{test: "bool match", val: true, valOther: true, match: true},
{test: "bool no match", val: true, valOther: false, match: false},
{test: "int match", val: int(10), valOther: int(10), match: true},
{test: "int no match", val: int(20), valOther: int(21), match: false},
{test: "int8 match", val: int8(10), valOther: int8(10), match: true},
{test: "int8 no match", val: int8(20), valOther: int8(21), match: false},
{test: "int16 match", val: int16(10), valOther: int16(10), match: true},
{test: "int16 no match", val: int16(20), valOther: int16(21), match: false},
{test: "int32 match", val: int32(10), valOther: int32(10), match: true},
{test: "int32 no match", val: int32(20), valOther: int32(21), match: false},
{test: "int64 match", val: int64(10), valOther: int64(10), match: true},
{test: "int64 no match", val: int64(20), valOther: int64(21), match: false},
{test: "uint match", val: uint(10), valOther: uint(10), match: true},
{test: "uint no match", val: uint(20), valOther: uint(21), match: false},
{test: "uint8 match", val: uint8(10), valOther: uint8(10), match: true},
{test: "uint8 no match", val: uint8(20), valOther: uint8(21), match: false},
{test: "uint16 match", val: uint16(10), valOther: uint16(10), match: true},
{test: "uint16 no match", val: uint16(20), valOther: uint16(21), match: false},
{test: "uint32 match", val: uint32(10), valOther: uint32(10), match: true},
{test: "uint32 no match", val: uint32(20), valOther: uint32(21), match: false},
{test: "uint64 match", val: uint64(10), valOther: uint64(10), match: true},
{test: "uint64 no match", val: uint64(20), valOther: uint64(21), match: false},
{test: "float32 match", val: float32(10.5), valOther: float32(10.5), match: true},
{test: "float32 no match", val: float32(20.5), valOther: float32(21.5), match: false},
{test: "float64 match", val: float64(10.5), valOther: float64(10.5), match: true},
{test: "float64 no match", val: float64(20.5), valOther: float64(21.5), match: false},
{test: "string match", val: "foo", valOther: "foo", match: true},
{test: "string no match", val: "foo", valOther: "Foo", match: false},
{test: "struct match", val: dataStruct{S: "foo"}, valOther: dataStruct{S: "foo"}, match: true},
{test: "struct no match", val: dataStruct{S: "foo"}, valOther: dataStruct{S: "Foo"}, match: false},
{test: "struct pointer match", val: dataStruct{S: "foo"}, valOther: &dataStruct{S: "foo"}, match: true},
}

for _, test := range tests {
for _, alg := range algorithms {
t.Run(test.test, func(t *testing.T) {
valHash, err := hash.ToHashString(alg.alg, test.val)
require.NoError(t, err)
hashParts := strings.Split(valHash, "=")
assert.Equal(t, string(alg.alg), hashParts[0])
valHashBytes, err := hex.DecodeString(hashParts[1])
require.NoError(t, err)
assert.Len(t, valHashBytes, alg.hashLen)
match, err := hash.CompareWithHashString(test.valOther, valHash)
require.NoError(t, err)
assert.Equal(t, test.match, match)
t.Parallel()
})
}
}
}
16 changes: 16 additions & 0 deletions pipeline-runner/internal/hash/sha256.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package hash

import "crypto/sha256"

const (
SHA256 Algorithm = "sha256"
)

func sum256(in []byte) []byte {
hashBytes := sha256.Sum256(in)
return hashBytes[:]
}

func init() {
registerProvider(SHA256, sum256)
}
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading