github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/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  	osi "github.com/sealerio/sealer/utils/os"
    22  
    23  	"github.com/sealerio/sealer/pkg/client/docker/auth"
    24  
    25  	"github.com/sealerio/sealer/common"
    26  	"github.com/sealerio/sealer/pkg/image/distributionutil"
    27  	v2 "github.com/sealerio/sealer/types/api/v2"
    28  )
    29  
    30  type RegistryChecker struct {
    31  	RegistryDomain string
    32  }
    33  
    34  func (r *RegistryChecker) Check(cluster *v2.Cluster, phase string) error {
    35  	if phase != PhasePre {
    36  		return nil
    37  	}
    38  
    39  	// checker the existence of the docker.json ;
    40  	authFile := common.DefaultRegistryAuthConfigDir()
    41  	if !osi.IsFileExist(authFile) {
    42  		return fmt.Errorf("registry auth info not found,please run 'sealer login' first")
    43  	}
    44  	// try to log in with auth info
    45  	svc, err := auth.NewDockerAuthService()
    46  	if err != nil {
    47  		return fmt.Errorf("failed to read default auth file %s: %v", authFile, err)
    48  	}
    49  
    50  	authConfig, err := svc.GetAuthByDomain(r.RegistryDomain)
    51  	if err != nil {
    52  		return fmt.Errorf("failed to get auth info, err: %s", err)
    53  	}
    54  
    55  	err = distributionutil.Login(context.Background(), &authConfig)
    56  	if err != nil {
    57  		return fmt.Errorf("failed to login %v: %v", r.RegistryDomain, err)
    58  	}
    59  	return nil
    60  }