github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/internal/cmdutil/util.go (about)

     1  // Copyright 2021 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cmdutil
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/GoogleContainerTools/kpt/internal/printer"
    22  	"github.com/GoogleContainerTools/kpt/pkg/live"
    23  	"k8s.io/kubectl/pkg/cmd/util"
    24  )
    25  
    26  // TODO(mortent): Combine this with the internal/util/cmdutil. Also consider
    27  // moving commands into a cmd package.
    28  
    29  // InstallResourceGroupCRD will install the ResourceGroup CRD into the cluster.
    30  // The function will block until the CRD is either installed and established, or
    31  // an error was encountered.
    32  // If the CRD could not be installed, an error of the type
    33  // ResourceGroupCRDInstallError will be returned.
    34  func InstallResourceGroupCRD(ctx context.Context, f util.Factory) error {
    35  	pr := printer.FromContextOrDie(ctx)
    36  	pr.Printf("installing inventory ResourceGroup CRD.\n")
    37  	err := (&live.ResourceGroupInstaller{
    38  		Factory: f,
    39  	}).InstallRG(ctx)
    40  	if err != nil {
    41  		return &ResourceGroupCRDInstallError{
    42  			Err: err,
    43  		}
    44  	}
    45  	return nil
    46  }
    47  
    48  // ResourceGroupCRDInstallError is an error that will be returned if the
    49  // ResourceGroup CRD can't be applied to the cluster.
    50  type ResourceGroupCRDInstallError struct {
    51  	Err error
    52  }
    53  
    54  func (*ResourceGroupCRDInstallError) Error() string {
    55  	return "error installing ResourceGroup crd"
    56  }
    57  
    58  func (i *ResourceGroupCRDInstallError) Unwrap() error {
    59  	return i.Err
    60  }
    61  
    62  // VerifyResourceGroupCRD verifies that the ResourceGroupCRD exists in
    63  // the cluster. If it doesn't an error of type NoResourceGroupCRDError
    64  // was returned.
    65  func VerifyResourceGroupCRD(f util.Factory) error {
    66  	if !live.ResourceGroupCRDApplied(f) {
    67  		return &NoResourceGroupCRDError{}
    68  	}
    69  	return nil
    70  }
    71  
    72  // NoResourceGroupCRDError is an error type that will be used when a
    73  // cluster doesn't have the ResourceGroup CRD installed.
    74  type NoResourceGroupCRDError struct{}
    75  
    76  func (*NoResourceGroupCRDError) Error() string {
    77  	return "type ResourceGroup not found"
    78  }
    79  
    80  // ResourceGroupCRDNotLatestError is an error type that will be used when a
    81  // cluster has a ResourceGroup CRD that doesn't match the
    82  // latest declaration.
    83  type ResourceGroupCRDNotLatestError struct {
    84  	Err error
    85  }
    86  
    87  func (e *ResourceGroupCRDNotLatestError) Error() string {
    88  	return fmt.Sprintf(
    89  		"Type ResourceGroup CRD needs update. Please make sure you have the permission "+
    90  			"to update CRD then run `kpt live install-resource-group`.\n %v", e.Err)
    91  }