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

[main] perform manifest transformation earlier #2083

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 7 additions & 7 deletions pkg/reconciler/kubernetes/tektoninstallerset/client/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ func (i *InstallerSetClient) checkSet(ctx context.Context, comp v1alpha1.TektonC
logger := logging.FromContext(ctx)

labelSelector := i.getSetLabels(isType)
logger.Infof("%v/%v: checking installer sets with labels: %v", i.resourceKind, isType, labelSelector)
logger.Debugf("%v/%v: checking installer sets with labels: %v", i.resourceKind, isType, labelSelector)

is, err := i.clientSet.List(ctx, v1.ListOptions{LabelSelector: labelSelector})
if err != nil {
return nil, err
}

logger.Infof("%v/%v: found %v installer sets", i.resourceKind, isType, len(is.Items))
logger.Debugf("%v/%v: found %v installer sets", i.resourceKind, isType, len(is.Items))

iSets := is.Items

if len(iSets) == 0 {
logger.Infof("%v/%v: installer sets not found", i.resourceKind, isType)
logger.Debugf("%v/%v: installer sets not found", i.resourceKind, isType)
return nil, ErrNotFound
}

Expand Down Expand Up @@ -83,7 +83,7 @@ func (i *InstallerSetClient) checkSet(ctx context.Context, comp v1alpha1.TektonC
logger.Errorf("%v/%v: meta check failed for installer type: %v", i.resourceKind, isType, err)
return iSets, err
}
logger.Infof("%v/%v: meta check passed", i.resourceKind, isType)
logger.Debugf("%v/%v: meta check passed", i.resourceKind, isType)

return iSets, nil
}
Expand All @@ -109,7 +109,7 @@ func verifyMainInstallerSets(iSets []v1alpha1.TektonInstallerSet) error {

func verifyMeta(resourceKind, isType string, logger *zap.SugaredLogger, set v1alpha1.TektonInstallerSet, comp v1alpha1.TektonComponent, releaseVersion string) error {
// Release Version Check
logger.Infof("%v/%v: release version check", resourceKind, isType)
logger.Debugf("%v/%v: release version check", resourceKind, isType)

rVel, ok := set.GetLabels()[v1alpha1.ReleaseVersionKey]
if !ok {
Expand All @@ -120,7 +120,7 @@ func verifyMeta(resourceKind, isType string, logger *zap.SugaredLogger, set v1al
}

// Target namespace check
logger.Infof("%v/%v: target namespace check", resourceKind, isType)
logger.Debugf("%v/%v: target namespace check", resourceKind, isType)

targetNamespace, ok := set.GetAnnotations()[v1alpha1.TargetNamespaceKey]
if !ok {
Expand All @@ -131,7 +131,7 @@ func verifyMeta(resourceKind, isType string, logger *zap.SugaredLogger, set v1al
}

// Spec Hash Check
logger.Infof("%v/%v: spec hash check", resourceKind, isType)
logger.Debugf("%v/%v: spec hash check", resourceKind, isType)

expectedHash, err := hash.Compute(comp.GetSpec())
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ import (

"github.com/tektoncd/operator/pkg/apis/operator/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"knative.dev/pkg/logging"
)

var deletePropagationPolicy = v1.DeletePropagationForeground
var deletePropagationPolicy = metav1.DeletePropagationForeground

func (i *InstallerSetClient) CleanupMainSet(ctx context.Context) error {
logger := logging.FromContext(ctx).With("kind", i.resourceKind, "type", InstallerTypeMain)
Expand All @@ -46,7 +45,7 @@ func (i *InstallerSetClient) CleanupMainSet(ctx context.Context) error {
// delete all static installerSet first and then deployment one
for _, is := range list.Items {
if strings.Contains(is.GetName(), InstallerSubTypeStatic) {
logger.Infof("deleting main-static installer set: %s", is.GetName())
logger.Debugf("deleting main-static installer set: %s", is.GetName())
err = i.clientSet.Delete(ctx, is.GetName(), metav1.DeleteOptions{
PropagationPolicy: &deletePropagationPolicy,
})
Expand All @@ -59,7 +58,7 @@ func (i *InstallerSetClient) CleanupMainSet(ctx context.Context) error {
// now delete all deployment installerSet
for _, is := range list.Items {
if strings.Contains(is.GetName(), InstallerSubTypeDeployment) {
logger.Infof("deleting main-deployment installer set: %s", is.GetName())
logger.Debugf("deleting main-deployment installer set: %s", is.GetName())
err = i.clientSet.Delete(ctx, is.GetName(), metav1.DeleteOptions{
PropagationPolicy: &deletePropagationPolicy,
})
Expand Down Expand Up @@ -116,7 +115,7 @@ func (i *InstallerSetClient) cleanup(ctx context.Context, isType string) error {
}

for _, is := range list.Items {
logger.Infof("deleting %s installer set: %s", isType, is.GetName())
logger.Debugf("deleting %s installer set: %s", isType, is.GetName())
err = i.clientSet.Delete(ctx, is.GetName(), metav1.DeleteOptions{
PropagationPolicy: &deletePropagationPolicy,
})
Expand Down
21 changes: 8 additions & 13 deletions pkg/reconciler/kubernetes/tektoninstallerset/client/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import (
"knative.dev/pkg/logging"
)

func (i *InstallerSetClient) create(ctx context.Context, comp v1alpha1.TektonComponent, manifest *mf.Manifest, filterAndTransform FilterAndTransform, isType string, customLabels map[string]string) ([]v1alpha1.TektonInstallerSet, error) {
func (i *InstallerSetClient) create(ctx context.Context, comp v1alpha1.TektonComponent, manifest *mf.Manifest, isType string, customLabels map[string]string) ([]v1alpha1.TektonInstallerSet, error) {
logger := logging.FromContext(ctx).With("kind", i.resourceKind, "type", isType)

if isType == InstallerTypeMain {
sets, err := i.makeMainSets(ctx, comp, manifest, filterAndTransform)
sets, err := i.makeMainSets(ctx, comp, manifest)
if err != nil {
logger.Errorf("installer set creation failed for main type: %v", err)
return sets, err
Expand All @@ -45,7 +45,7 @@ func (i *InstallerSetClient) create(ctx context.Context, comp v1alpha1.TektonCom
kind := strings.ToLower(strings.TrimPrefix(i.resourceKind, "Tekton"))
isName := fmt.Sprintf("%s-%s-", kind, isType)

iS, err := i.makeInstallerSet(ctx, comp, manifest, filterAndTransform, isName, isType, customLabels)
iS, err := i.makeInstallerSet(ctx, comp, manifest, isName, isType, customLabels)
if err != nil {
return nil, err
}
Expand All @@ -57,14 +57,14 @@ func (i *InstallerSetClient) create(ctx context.Context, comp v1alpha1.TektonCom
return []v1alpha1.TektonInstallerSet{*iS}, nil
}

func (i *InstallerSetClient) makeMainSets(ctx context.Context, comp v1alpha1.TektonComponent, manifest *mf.Manifest, filterAndTransform FilterAndTransform) ([]v1alpha1.TektonInstallerSet, error) {
func (i *InstallerSetClient) makeMainSets(ctx context.Context, comp v1alpha1.TektonComponent, manifest *mf.Manifest) ([]v1alpha1.TektonInstallerSet, error) {
staticManifest := manifest.Filter(mf.Not(mf.ByKind("Deployment")), mf.Not(mf.ByKind("Service")))
deploymentManifest := manifest.Filter(mf.Any(mf.ByKind("Deployment"), mf.ByKind("Service")))

kind := strings.ToLower(strings.TrimPrefix(i.resourceKind, "Tekton"))
staticName := fmt.Sprintf("%s-%s-%s-", kind, InstallerTypeMain, InstallerSubTypeStatic)

staticIS, err := i.makeInstallerSet(ctx, comp, &staticManifest, filterAndTransform, staticName, InstallerTypeMain, nil)
staticIS, err := i.makeInstallerSet(ctx, comp, &staticManifest, staticName, InstallerTypeMain, nil)
if err != nil {
return nil, err
}
Expand All @@ -78,7 +78,7 @@ func (i *InstallerSetClient) makeMainSets(ctx context.Context, comp v1alpha1.Tek
}

deployName := fmt.Sprintf("%s-%s-%s-", kind, InstallerTypeMain, InstallerSubTypeDeployment)
deploymentIS, err := i.makeInstallerSet(ctx, comp, &deploymentManifest, filterAndTransform, deployName, InstallerTypeMain, nil)
deploymentIS, err := i.makeInstallerSet(ctx, comp, &deploymentManifest, deployName, InstallerTypeMain, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -109,17 +109,12 @@ func (i *InstallerSetClient) waitForStatus(ctx context.Context, set *v1alpha1.Te
return nil
}

func (i *InstallerSetClient) makeInstallerSet(ctx context.Context, comp v1alpha1.TektonComponent, manifest *mf.Manifest, filterAndTransform FilterAndTransform, isName, isType string, customLabels map[string]string) (*v1alpha1.TektonInstallerSet, error) {
func (i *InstallerSetClient) makeInstallerSet(ctx context.Context, comp v1alpha1.TektonComponent, manifest *mf.Manifest, isName, isType string, customLabels map[string]string) (*v1alpha1.TektonInstallerSet, error) {
specHash, err := hash.Compute(comp.GetSpec())
if err != nil {
return nil, err
}

transformedMf, err := filterAndTransform(ctx, manifest, comp)
if err != nil {
return nil, err
}

// get default labels of installerset
labels := i.getDefaultLabels(isType)
// append custom labels
Expand All @@ -139,7 +134,7 @@ func (i *InstallerSetClient) makeInstallerSet(ctx context.Context, comp v1alpha1
OwnerReferences: []metav1.OwnerReference{ownerRef},
},
Spec: v1alpha1.TektonInstallerSetSpec{
Manifests: transformedMf.Resources(),
Manifests: manifest.Resources(),
},
}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
mf "github.com/manifestival/manifestival"
"github.com/tektoncd/operator/pkg/apis/operator/v1alpha1"
"github.com/tektoncd/operator/pkg/client/clientset/versioned/fake"
"github.com/tektoncd/operator/pkg/reconciler/common"
fake2 "github.com/tektoncd/operator/pkg/reconciler/kubernetes/tektoninstallerset/client/fake"
"gotest.tools/v3/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -111,7 +110,7 @@ func TestInstallerSetClient_Create(t *testing.T) {
client = NewInstallerSetClient(fakeClient, releaseVersion, "test-version", v1alpha1.KindTektonTrigger, &testMetrics{})
}

iSs, gotErr := client.create(ctx, comp, &manifest, filterAndTransform(common.NoExtension(ctx)), tt.setType, nil)
iSs, gotErr := client.create(ctx, comp, &manifest, tt.setType, nil)

if tt.wantErr != nil {
assert.Equal(t, gotErr, tt.wantErr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ var (
func (i *InstallerSetClient) VersionedClusterTaskSet(ctx context.Context, comp v1alpha1.TektonComponent, manifest *mf.Manifest, filterAndTransform FilterAndTransform) error {
logger := logging.FromContext(ctx)

// perform transformation
manifestUpdated, err := filterAndTransform(ctx, manifest, comp)
if err != nil {
logger.Errorw("error on transforming a manifest",
"component", comp.GroupVersionKind().String(),
"componentName", comp.GetName(),
)
return err
}

setType := InstallerTypeCustom + "-" + strings.ToLower(versionedClusterTaskInstallerSet)
versionedClusterTaskLS := v1.LabelSelector{
MatchLabels: map[string]string{
Expand All @@ -55,7 +65,7 @@ func (i *InstallerSetClient) VersionedClusterTaskSet(ctx context.Context, comp v
}

if len(is.Items) == 0 {
vctSet, err := i.makeInstallerSet(ctx, comp, manifest, filterAndTransform, "addon-versioned-clustertasks", setType, nil)
vctSet, err := i.makeInstallerSet(ctx, comp, manifestUpdated, "addon-versioned-clustertasks", setType, nil)
if err != nil {
return err
}
Expand Down
30 changes: 20 additions & 10 deletions pkg/reconciler/kubernetes/tektoninstallerset/client/main_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,25 @@ func (i *InstallerSetClient) MainSet(ctx context.Context, comp v1alpha1.TektonCo
logger := logging.FromContext(ctx)
setType := InstallerTypeMain

// perform transformation
manifestUpdated, err := filterAndTransform(ctx, manifest, comp)
if err != nil {
logger.Errorw("error on transforming a manifest",
"component", comp.GroupVersionKind().String(),
"componentName", comp.GetName(),
)
return err
}

sets, err := i.checkSet(ctx, comp, setType)
if err == nil {
logger.Infof("%v/%v: found %v installer sets", i.resourceKind, setType, len(sets))
logger.Debugf("%v/%v: found %v installer sets", i.resourceKind, setType, len(sets))
}

switch err {
case ErrNotFound:
logger.Infof("%v/%v: installer set not found, creating", i.resourceKind, setType)
sets, err = i.create(ctx, comp, manifest, filterAndTransform, setType, nil)
logger.Debugf("%v/%v: installer set not found, creating", i.resourceKind, setType)
sets, err = i.create(ctx, comp, manifestUpdated, setType, nil)
if err != nil {
logger.Errorf("%v/%v: failed to create main installer set: %v", i.resourceKind, setType, err)
return err
Expand All @@ -54,7 +64,7 @@ func (i *InstallerSetClient) MainSet(ctx context.Context, comp v1alpha1.TektonCo
}

case ErrInvalidState, ErrNsDifferent, ErrVersionDifferent:
logger.Infof("%v/%v: installer set not in valid state : %v, cleaning up!", i.resourceKind, setType, err)
logger.Debugf("%v/%v: installer set not in valid state : %v, cleaning up!", i.resourceKind, setType, err)
if err := i.CleanupMainSet(ctx); err != nil {
logger.Errorf("%v/%v: failed to cleanup main installer set: %v", i.resourceKind, setType, err)
return err
Expand All @@ -65,18 +75,18 @@ func (i *InstallerSetClient) MainSet(ctx context.Context, comp v1alpha1.TektonCo
} else {
markComponentStatus(comp, v1alpha1.Reinstalling)
}
logger.Infof("%v/%v: returning, will create main installer sets in further reconcile", i.resourceKind, setType)
logger.Debugf("%v/%v: returning, will create main installer sets in further reconcile", i.resourceKind, setType)
return v1alpha1.REQUEUE_EVENT_AFTER

case ErrUpdateRequired:
logger.Infof("%v/%v: updating installer set", i.resourceKind, setType)
sets, err = i.update(ctx, comp, sets, manifest, filterAndTransform, setType)
logger.Debugf("%v/%v: updating installer set", i.resourceKind, setType)
sets, err = i.update(ctx, comp, sets, manifestUpdated, setType)
if err != nil {
logger.Errorf("%v/%v: update failed : %v", i.resourceKind, setType, err)
return err
}
case ErrSetsInDeletionState:
logger.Infof("%v/%v: %v", i.resourceKind, setType, err)
logger.Debugf("%v/%v: %v", i.resourceKind, setType, err)
return v1alpha1.REQUEUE_EVENT_AFTER
}

Expand All @@ -97,12 +107,12 @@ func (i *InstallerSetClient) statusCheck(logger *zap.SugaredLogger, setType stri
for _, set := range sets {
ready := set.Status.GetCondition(apis.ConditionReady)
if ready.IsUnknown() {
logger.Infof("%v/%v: installer set %v status not set, wait !", i.resourceKind, setType, set.GetName())
logger.Debugf("%v/%v: installer set %v status not set, wait !", i.resourceKind, setType, set.GetName())
return v1alpha1.REQUEUE_EVENT_AFTER
}
if !ready.IsTrue() {
msg := fmt.Sprintf("%v/%v: installer set not ready, will retry: %v", i.resourceKind, setType, ready.Message)
logger.Infof(msg)
logger.Debugf(msg)
return fmt.Errorf(msg)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,67 @@ import (
)

func (i *InstallerSetClient) PostSet(ctx context.Context, comp v1alpha1.TektonComponent, manifest *mf.Manifest, filterAndTransform FilterAndTransform) error {
return i.createSet(ctx, comp, InstallerTypePost, manifest, filterAndTransform, nil)
return i.applyTransformationAndCreateSet(ctx, comp, InstallerTypePost, manifest, filterAndTransform, nil)
}

func (i *InstallerSetClient) PreSet(ctx context.Context, comp v1alpha1.TektonComponent, manifest *mf.Manifest, filterAndTransform FilterAndTransform) error {
return i.createSet(ctx, comp, InstallerTypePre, manifest, filterAndTransform, nil)
return i.applyTransformationAndCreateSet(ctx, comp, InstallerTypePre, manifest, filterAndTransform, nil)
}

func (i *InstallerSetClient) CustomSet(ctx context.Context, comp v1alpha1.TektonComponent, customName string, manifest *mf.Manifest, filterAndTransform FilterAndTransform, customLabels map[string]string) error {
setType := InstallerTypeCustom + "-" + strings.ToLower(customName)
return i.createSet(ctx, comp, setType, manifest, filterAndTransform, customLabels)
return i.applyTransformationAndCreateSet(ctx, comp, setType, manifest, filterAndTransform, customLabels)
}

func (i *InstallerSetClient) createSet(ctx context.Context, comp v1alpha1.TektonComponent, setType string, manifest *mf.Manifest, filterAndTransform FilterAndTransform, customLabels map[string]string) error {
func (i *InstallerSetClient) applyTransformationAndCreateSet(ctx context.Context, comp v1alpha1.TektonComponent, setType string, manifest *mf.Manifest, filterAndTransform FilterAndTransform, customLabels map[string]string) error {
// perform transformation
manifestUpdated, err := filterAndTransform(ctx, manifest, comp)
if err != nil {
logger := logging.FromContext(ctx)
logger.Errorw("error on transforming a manifest",
"component", comp.GroupVersionKind().String(),
"componentName", comp.GetName(),
)
return err
}
return i.createSet(ctx, comp, setType, manifestUpdated, customLabels)
}

func (i *InstallerSetClient) createSet(ctx context.Context, comp v1alpha1.TektonComponent, setType string, manifest *mf.Manifest, customLabels map[string]string) error {
logger := logging.FromContext(ctx)

sets, err := i.checkSet(ctx, comp, setType)
if err == nil {
logger.Infof("%v/%v: found %v installer sets", i.resourceKind, setType, len(sets))
logger.Debugf("%v/%v: found %v installer sets", i.resourceKind, setType, len(sets))
}

switch err {
case ErrNotFound:
logger.Infof("%v/%v: installer set not found, creating", i.resourceKind, setType)
sets, err = i.create(ctx, comp, manifest, filterAndTransform, setType, customLabels)
logger.Debugf("%v/%v: installer set not found, creating", i.resourceKind, setType)
sets, err = i.create(ctx, comp, manifest, setType, customLabels)
if err != nil {
logger.Errorf("%v/%v: failed to create installer set: %v", i.resourceKind, setType, err)
return err
}

case ErrInvalidState, ErrNsDifferent, ErrVersionDifferent:
logger.Infof("%v/%v: installer set not in valid state : %v, cleaning up!", i.resourceKind, setType, err)
logger.Debugf("%v/%v: installer set not in valid state : %v, cleaning up!", i.resourceKind, setType, err)
if err := i.CleanupSet(ctx, setType); err != nil {
logger.Errorf("%v/%v: failed to cleanup installer set: %v", i.resourceKind, setType, err)
return nil
}
logger.Infof("%v/%v: returning, will create installer sets in further reconcile", i.resourceKind, setType)
logger.Debugf("%v/%v: returning, will create installer sets in further reconcile", i.resourceKind, setType)
return v1alpha1.REQUEUE_EVENT_AFTER

case ErrUpdateRequired:
logger.Infof("%v/%v: updating installer set", i.resourceKind, setType)
sets, err = i.update(ctx, comp, sets, manifest, filterAndTransform, setType)
logger.Debugf("%v/%v: updating installer set", i.resourceKind, setType)
sets, err = i.update(ctx, comp, sets, manifest, setType)
if err != nil {
logger.Errorf("%v/%v: update failed : %v", i.resourceKind, setType, err)
return err
}
case ErrSetsInDeletionState:
logger.Infof("%v/%v: %v", i.resourceKind, setType, err)
logger.Debugf("%v/%v: %v", i.resourceKind, setType, err)
return v1alpha1.REQUEUE_EVENT_AFTER
}

Expand Down
Loading