-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates tektonPipeline to make it independent of tektonConfig
- With the addition of operator version config map getting created in tektonConfig, the targetNamespace was getting created in tektonConfig reconciler, but if tektonConfig is not installed and tektonPipeline is supposed to installed alone then it won't be installed as it would miss the targetNamespace. So this patch checks for the targetNamespace in tektonPipeline reconciler, if it is not present then it creates the targetNamespace and also the operator version config map i.e. makes tektonPipeline independent of tektonConfig Signed-off-by: Puneet Punamiya <[email protected]>
- Loading branch information
1 parent
bc9d220
commit 277d2cd
Showing
5 changed files
with
254 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2022 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 ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
|
||
mf "github.com/manifestival/manifestival" | ||
"github.com/tektoncd/operator/pkg/apis/operator/v1alpha1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
func CreateTargetNamespace(ctx context.Context, labels map[string]string, obj v1alpha1.TektonComponent, kubeClientSet kubernetes.Interface) error { | ||
ownerRef := *metav1.NewControllerRef(obj, obj.GroupVersionKind()) | ||
namespace := &corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: obj.GetSpec().GetTargetNamespace(), | ||
Labels: map[string]string{ | ||
"operator.tekton.dev/targetNamespace": "true", | ||
}, | ||
OwnerReferences: []metav1.OwnerReference{ownerRef}, | ||
}, | ||
} | ||
|
||
if len(labels) > 0 { | ||
for key, value := range labels { | ||
namespace.Labels[key] = value | ||
} | ||
} | ||
|
||
if _, err := kubeClientSet.CoreV1().Namespaces().Create(ctx, namespace, metav1.CreateOptions{}); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func CreateOperatorVersionConfigMap(manifest mf.Manifest, obj v1alpha1.TektonComponent) error { | ||
koDataDir := os.Getenv(KoEnvKey) | ||
operatorDir := filepath.Join(koDataDir, "info") | ||
|
||
if err := AppendManifest(&manifest, operatorDir); err != nil { | ||
return err | ||
} | ||
|
||
manifest, err := manifest.Transform( | ||
mf.InjectNamespace(obj.GetSpec().GetTargetNamespace()), | ||
mf.InjectOwner(obj), | ||
) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = manifest.Apply(); 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
Copyright 2022 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 ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
mf "github.com/manifestival/manifestival" | ||
"github.com/tektoncd/operator/pkg/apis/operator/v1alpha1" | ||
"gotest.tools/v3/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
type fakeClient struct { | ||
err error | ||
getErr error | ||
createErr error | ||
resourcesExist bool | ||
creates []unstructured.Unstructured | ||
deletes []unstructured.Unstructured | ||
} | ||
|
||
var configMap = namespacedResource("v1", "ConfigMap", "test-ns", "operators-info") | ||
|
||
func (f *fakeClient) Get(obj *unstructured.Unstructured) (*unstructured.Unstructured, error) { | ||
var resource *unstructured.Unstructured | ||
if f.resourcesExist { | ||
resource = &unstructured.Unstructured{} | ||
} | ||
return resource, f.getErr | ||
} | ||
|
||
func (f *fakeClient) Create(obj *unstructured.Unstructured, options ...mf.ApplyOption) error { | ||
obj.SetAnnotations(nil) // Deleting the extra annotation. Irrelevant for the test. | ||
f.creates = append(f.creates, *obj) | ||
return f.createErr | ||
} | ||
|
||
func (f *fakeClient) Delete(obj *unstructured.Unstructured, options ...mf.DeleteOption) error { | ||
f.deletes = append(f.deletes, *obj) | ||
return f.err | ||
} | ||
|
||
func (f *fakeClient) Update(obj *unstructured.Unstructured, options ...mf.ApplyOption) error { | ||
return f.err | ||
} | ||
|
||
func TestCreateTargetNamespace(t *testing.T) { | ||
targetNamespace := "test-ns" | ||
component := &v1alpha1.TektonPipeline{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-name", | ||
}, | ||
Spec: v1alpha1.TektonPipelineSpec{ | ||
CommonSpec: v1alpha1.CommonSpec{ | ||
TargetNamespace: targetNamespace, | ||
}, | ||
}, | ||
} | ||
fakeClientset := fake.NewSimpleClientset() | ||
|
||
err := CreateTargetNamespace(context.Background(), nil, component, fakeClientset) | ||
assert.Equal(t, err, nil) | ||
|
||
ns, err := fakeClientset.CoreV1().Namespaces().Get(context.Background(), targetNamespace, metav1.GetOptions{}) | ||
assert.Equal(t, err, nil) | ||
assert.Equal(t, ns.ObjectMeta.Name, targetNamespace) | ||
} | ||
|
||
func TestTargetNamespaceNotFound(t *testing.T) { | ||
component := &v1alpha1.TektonPipeline{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-name", | ||
}, | ||
Spec: v1alpha1.TektonPipelineSpec{ | ||
CommonSpec: v1alpha1.CommonSpec{}, | ||
}, | ||
} | ||
fakeClientset := fake.NewSimpleClientset() | ||
|
||
err := CreateTargetNamespace(context.Background(), nil, component, fakeClientset) | ||
assert.Equal(t, err, nil) | ||
|
||
_, err = fakeClientset.CoreV1().Namespaces().Get(context.Background(), "foo", metav1.GetOptions{}) | ||
assert.Equal(t, err.Error(), `namespaces "foo" not found`) | ||
} | ||
|
||
func TestOperatorVersionCreateConfigMap(t *testing.T) { | ||
os.Setenv(KoEnvKey, "testdata/kodata") | ||
defer os.Unsetenv(KoEnvKey) | ||
|
||
targetNamespace := "test-ns" | ||
component := &v1alpha1.TektonPipeline{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-name", | ||
}, | ||
Spec: v1alpha1.TektonPipelineSpec{ | ||
CommonSpec: v1alpha1.CommonSpec{ | ||
TargetNamespace: targetNamespace, | ||
}, | ||
}, | ||
} | ||
fakeClientset := fake.NewSimpleClientset() | ||
|
||
err := CreateTargetNamespace(context.Background(), nil, component, fakeClientset) | ||
assert.Equal(t, err, nil) | ||
|
||
var manifest mf.Manifest | ||
|
||
client := &fakeClient{} | ||
manifest, err = mf.ManifestFrom(mf.Slice(manifest.Resources()), mf.UseClient(client)) | ||
assert.Equal(t, err, nil) | ||
|
||
err = CreateOperatorVersionConfigMap(manifest, component) | ||
assert.Equal(t, err, nil) | ||
|
||
want := []unstructured.Unstructured{configMap} | ||
|
||
if len(want) != len(client.creates) { | ||
t.Fatalf("Unexpected creates: %s", fmt.Sprintf("(-got, +want): %s", cmp.Diff(client.creates, want))) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
pkg/reconciler/common/testdata/kodata/info/test-create-operator-version-config-map.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,24 @@ | ||
# Copyright 2022 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 | ||
# | ||
# https://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. | ||
|
||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: operators-info | ||
labels: | ||
app.kubernetes.io/instance: default | ||
data: | ||
# Contains operator version which can be queried by external | ||
# tools such as CLI. | ||
version: 'devel' |
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