github.com/oam-dev/kubevela@v1.9.11/cmd/core/app/hooks/pre_start_hook.go (about)

     1  /*
     2  Copyright 2022 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package hooks
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"time"
    23  
    24  	"github.com/kubevela/pkg/util/compression"
    25  	"github.com/kubevela/pkg/util/k8s"
    26  	"github.com/kubevela/pkg/util/singleton"
    27  	"k8s.io/apiserver/pkg/util/feature"
    28  	"k8s.io/klog/v2"
    29  	"sigs.k8s.io/controller-runtime/pkg/client"
    30  
    31  	"github.com/oam-dev/kubevela/apis/core.oam.dev/common"
    32  	"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
    33  	"github.com/oam-dev/kubevela/apis/types"
    34  	"github.com/oam-dev/kubevela/pkg/features"
    35  	"github.com/oam-dev/kubevela/pkg/oam"
    36  )
    37  
    38  // PreStartHook hook that should be run before controller start working
    39  type PreStartHook interface {
    40  	Run(ctx context.Context) error
    41  }
    42  
    43  // SystemCRDValidationHook checks if the crd in the system are valid to run the current controller
    44  type SystemCRDValidationHook struct {
    45  	client.Client
    46  }
    47  
    48  // NewSystemCRDValidationHook .
    49  func NewSystemCRDValidationHook() PreStartHook {
    50  	return &SystemCRDValidationHook{Client: singleton.KubeClient.Get()}
    51  }
    52  
    53  // Run .
    54  func (in *SystemCRDValidationHook) Run(ctx context.Context) error {
    55  	if feature.DefaultMutableFeatureGate.Enabled(features.ZstdApplicationRevision) ||
    56  		feature.DefaultMutableFeatureGate.Enabled(features.GzipApplicationRevision) {
    57  		appRev := &v1beta1.ApplicationRevision{}
    58  		appRev.Name = fmt.Sprintf("core.pre-check.%d", time.Now().UnixNano())
    59  		appRev.Namespace = k8s.GetRuntimeNamespace()
    60  		key := client.ObjectKeyFromObject(appRev)
    61  		appRev.SetLabels(map[string]string{oam.LabelPreCheck: types.VelaCoreName})
    62  		appRev.Spec.Application.Name = appRev.Name
    63  		appRev.Spec.Application.Spec.Components = []common.ApplicationComponent{}
    64  		if feature.DefaultMutableFeatureGate.Enabled(features.ZstdApplicationRevision) {
    65  			appRev.Spec.Compression.SetType(compression.Zstd)
    66  		} else if feature.DefaultMutableFeatureGate.Enabled(features.GzipApplicationRevision) {
    67  			appRev.Spec.Compression.SetType(compression.Gzip)
    68  		}
    69  		if err := in.Client.Create(ctx, appRev); err != nil {
    70  			return err
    71  		}
    72  		defer func() {
    73  			if err := in.Client.DeleteAllOf(ctx, &v1beta1.ApplicationRevision{},
    74  				client.InNamespace(types.DefaultKubeVelaNS),
    75  				client.MatchingLabels{oam.LabelPreCheck: types.VelaCoreName}); err != nil {
    76  				klog.Errorf("failed to recycle pre-check ApplicationRevision: %w", err)
    77  			}
    78  		}()
    79  		if err := in.Client.Get(ctx, key, appRev); err != nil {
    80  			return err
    81  		}
    82  		if appRev.Spec.Application.Name != appRev.Name {
    83  			return fmt.Errorf("the ApplicationRevision CRD is not updated. Compression cannot be used. Please upgrade your CRD to latest ones")
    84  		}
    85  	}
    86  	return nil
    87  }