github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/checker/registry_checker.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     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 checker
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/alibaba/sealer/common"
    22  	"github.com/alibaba/sealer/pkg/image/distributionutil"
    23  	v2 "github.com/alibaba/sealer/types/api/v2"
    24  	"github.com/alibaba/sealer/utils"
    25  )
    26  
    27  type RegistryChecker struct {
    28  	RegistryDomain string
    29  }
    30  
    31  func (r *RegistryChecker) Check(cluster *v2.Cluster, phase string) error {
    32  	if phase != PhasePre {
    33  		return nil
    34  	}
    35  
    36  	// checker the existence of the docker.json ;
    37  	authFile := common.DefaultRegistryAuthConfigDir()
    38  	if !utils.IsFileExist(authFile) {
    39  		return fmt.Errorf("registry auth info not found,please run 'sealer login' first")
    40  	}
    41  	// try to login with auth info
    42  	authConfig, err := utils.GetDockerAuthInfoFromDocker(r.RegistryDomain)
    43  	if err != nil {
    44  		return fmt.Errorf("failed to get auth info, err: %s", err)
    45  	}
    46  
    47  	err = distributionutil.Login(context.Background(), &authConfig)
    48  	if err != nil {
    49  		return fmt.Errorf("%v authentication failed", r.RegistryDomain)
    50  	}
    51  	return nil
    52  }
    53  
    54  func NewRegistryChecker(registryDomain string) Interface {
    55  	return &RegistryChecker{RegistryDomain: registryDomain}
    56  }