Skip to content

Commit

Permalink
Updates tektonPipeline to make it independent of tektonConfig
Browse files Browse the repository at this point in the history
  - 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
PuneetPunamiya committed Feb 2, 2022
1 parent 24070f0 commit 38bd9a2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 27 deletions.
54 changes: 54 additions & 0 deletions pkg/reconciler/common/targetnamespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package common

import (
"context"
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"
"os"
"path/filepath"
)

func CreateTargetNamespace(labels map[string]string, obj v1alpha1.TektonComponent, kubeClientSet kubernetes.Interface) error {
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: obj.GetSpec().GetTargetNamespace(),
Labels: map[string]string{
"operator.tekton.dev/targetNamespace": "true",
},
},
}
for key, value := range labels {
namespace.Labels[key] = value
}

if _, err := kubeClientSet.CoreV1().Namespaces().Create(context.TODO(), 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
}
9 changes: 9 additions & 0 deletions pkg/reconciler/kubernetes/tektonpipeline/tektonpipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,17 @@ func (r *Reconciler) targetNamespaceCheck(ctx context.Context, tp *v1alpha1.Tekt
ns, err := r.kubeClientSet.CoreV1().Namespaces().Get(ctx, tp.GetSpec().GetTargetNamespace(), metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
if err := common.CreateTargetNamespace(labels, tp, r.kubeClientSet); err != nil {
return err
}
if err := common.CreateOperatorVersionConfigMap(r.manifest, tp); err != nil {
return err
}
return nil
} else {
return err
}

}
for key, value := range labels {
ns.Labels[key] = value
Expand Down
32 changes: 5 additions & 27 deletions pkg/reconciler/shared/tektonconfig/tektonconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ package tektonconfig
import (
"context"
"fmt"
"os"
"path/filepath"

corev1 "k8s.io/api/core/v1"

mf "github.com/manifestival/manifestival"
Expand Down Expand Up @@ -99,7 +96,7 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, tc *v1alpha1.TektonConfi
return err
}

if err := r.createOperatorVersionConfigMap(tc); err != nil {
if err := common.CreateOperatorVersionConfigMap(r.manifest, tc); err != nil {
return err
}

Expand Down Expand Up @@ -158,29 +155,6 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, tc *v1alpha1.TektonConfi
return nil
}

func (r *Reconciler) createOperatorVersionConfigMap(tc *v1alpha1.TektonConfig) error {
koDataDir := os.Getenv(common.KoEnvKey)
operatorDir := filepath.Join(koDataDir, "info")

if err := common.AppendManifest(&r.manifest, operatorDir); err != nil {
return err
}

manifest, err := r.manifest.Transform(
mf.InjectNamespace(tc.GetSpec().GetTargetNamespace()),
mf.InjectOwner(tc),
)
if err != nil {
return err
}

if err = manifest.Apply(); err != nil {
return err
}

return nil
}

func (r *Reconciler) ensureTargetNamespaceExists(ctx context.Context, tc *v1alpha1.TektonConfig) error {

ns, err := r.kubeClientSet.CoreV1().Namespaces().List(ctx, metav1.ListOptions{
Expand All @@ -205,6 +179,10 @@ func (r *Reconciler) ensureTargetNamespaceExists(ctx context.Context, tc *v1alph
}
}
} else {
labels := map[string]string{"operator.tekton.dev/targetNamespace": "true"}
if err := common.CreateTargetNamespace(labels, tc, r.kubeClientSet); err != nil {
return err
}
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: tc.GetSpec().GetTargetNamespace(),
Expand Down

0 comments on commit 38bd9a2

Please sign in to comment.