-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup: moves common code for components to common pkg
Signed-off-by: Shivam Mukhade <[email protected]>
- Loading branch information
Shivam Mukhade
committed
Sep 15, 2021
1 parent
00ed44c
commit 1b6fed2
Showing
7 changed files
with
187 additions
and
62 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
pkg/reconciler/common/testdata/test-fetch-version-from-crds.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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
apiVersion: apiextensions.k8s.io/v1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
name: tasks.tekton.dev | ||
labels: | ||
app.kubernetes.io/instance: default | ||
app.kubernetes.io/part-of: tekton-pipelines | ||
pipeline.tekton.dev/release: "devel" | ||
version: "devel" | ||
spec: | ||
group: tekton.dev | ||
preserveUnknownFields: false | ||
names: | ||
kind: Task | ||
plural: tasks | ||
categories: | ||
- tekton | ||
- tekton-pipelines | ||
scope: Namespaced | ||
versions: [] | ||
|
||
--- | ||
apiVersion: apiextensions.k8s.io/v1 | ||
kind: CustomResourceDefinition | ||
metadata: | ||
name: pipelines.tekton.dev | ||
labels: | ||
app.kubernetes.io/instance: default | ||
app.kubernetes.io/part-of: tekton-pipelines | ||
pipeline.tekton.dev/release: "devel" | ||
version: "devel" | ||
spec: | ||
group: tekton.dev | ||
preserveUnknownFields: false | ||
names: | ||
kind: Pipeline | ||
plural: pipelines | ||
categories: | ||
- tekton | ||
- tekton-pipelines | ||
scope: Namespaced | ||
versions: [] |
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,55 @@ | ||
/* | ||
Copyright 2021 The Tekton Authors | ||
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 common | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/json" | ||
"fmt" | ||
|
||
mf "github.com/manifestival/manifestival" | ||
) | ||
|
||
// FetchVersionFromCRD finds the component version from the crd labels, it looks for the | ||
// label on the first crd it finds after filtering | ||
// It will return error if crds are not found in manifest or label is not found | ||
func FetchVersionFromCRD(manifest mf.Manifest, releaseLabel string) (string, error) { | ||
crds := manifest.Filter(mf.CRDs) | ||
if len(crds.Resources()) == 0 { | ||
return "", fmt.Errorf("failed to find crds to get release version") | ||
} | ||
|
||
crd := crds.Resources()[0] | ||
version, ok := crd.GetLabels()[releaseLabel] | ||
if !ok { | ||
return version, fmt.Errorf("failed to find release label on crd") | ||
} | ||
|
||
return version, nil | ||
} | ||
|
||
// ComputeHashOf generates an unique hash/string for the | ||
// object pass to it. | ||
func ComputeHashOf(obj interface{}) (string, error) { | ||
h := sha256.New() | ||
d, err := json.Marshal(obj) | ||
if err != nil { | ||
return "", err | ||
} | ||
h.Write(d) | ||
return fmt.Sprintf("%x", h.Sum(nil)), 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,84 @@ | ||
/* | ||
Copyright 2021 The Tekton Authors | ||
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 common | ||
|
||
import ( | ||
"path" | ||
"testing" | ||
|
||
mf "github.com/manifestival/manifestival" | ||
"github.com/tektoncd/operator/pkg/apis/operator/v1alpha1" | ||
) | ||
|
||
func TestFetchVersionFromCRD(t *testing.T) { | ||
|
||
testData := path.Join("testdata", "test-fetch-version-from-crds.yaml") | ||
manifest, err := mf.ManifestFrom(mf.Recursive(testData)) | ||
assertNoEror(t, err) | ||
|
||
version, err := FetchVersionFromCRD(manifest, "pipeline.tekton.dev/release") | ||
if err != nil { | ||
t.Fatal("Unexpected Error: ", err) | ||
} | ||
|
||
if version != "devel" { | ||
t.Fatal("invalid label fetched from crd: ", version) | ||
} | ||
} | ||
|
||
func TestComputeHashOf(t *testing.T) { | ||
tp := &v1alpha1.TektonPipeline{ | ||
Spec: v1alpha1.TektonPipelineSpec{ | ||
CommonSpec: v1alpha1.CommonSpec{TargetNamespace: "tekton"}, | ||
Config: v1alpha1.Config{ | ||
NodeSelector: map[string]string{ | ||
"abc": "xyz", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
hash, err := ComputeHashOf(tp.Spec) | ||
if err != nil { | ||
t.Fatal("unexpected error while computing hash of obj") | ||
} | ||
|
||
// Again, calculate the hash without changing object | ||
|
||
hash2, err := ComputeHashOf(tp.Spec) | ||
if err != nil { | ||
t.Fatal("unexpected error while computing hash of obj") | ||
} | ||
|
||
if hash != hash2 { | ||
t.Fatal("hash changed without changing the object") | ||
} | ||
|
||
// Now, change the object | ||
|
||
tp.Spec.TargetNamespace = "changed" | ||
|
||
hash3, err := ComputeHashOf(tp.Spec) | ||
if err != nil { | ||
t.Fatal("unexpected error while computing hash of obj") | ||
} | ||
|
||
// Hash should be changed now | ||
if hash == hash3 { | ||
t.Fatal("hash not changed after changing the object") | ||
} | ||
} |
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