github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/runner/v1/apply.go (about)

     1  /*
     2  Copyright 2021 The Skaffold 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 v1
    18  
    19  import (
    20  	"context"
    21  	"io"
    22  	"time"
    23  
    24  	deployutil "github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/util"
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event"
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation"
    28  )
    29  
    30  // Apply sends Kubernetes manifests to the cluster.
    31  func (r *SkaffoldRunner) Apply(ctx context.Context, out io.Writer) error {
    32  	if err := r.applyResources(ctx, out, nil, nil); err != nil {
    33  		return err
    34  	}
    35  
    36  	statusCheckOut, postStatusCheckFn, err := deployutil.WithStatusCheckLogFile(time.Now().Format(deployutil.TimeFormat)+".log", out, r.runCtx.Muted())
    37  	postStatusCheckFn()
    38  	if err != nil {
    39  		return err
    40  	}
    41  	sErr := r.deployer.GetStatusMonitor().Check(ctx, statusCheckOut)
    42  	return sErr
    43  }
    44  
    45  func (r *SkaffoldRunner) applyResources(ctx context.Context, out io.Writer, artifacts, localImages []graph.Artifact) error {
    46  	deployOut, postDeployFn, err := deployutil.WithLogFile(time.Now().Format(deployutil.TimeFormat)+".log", out, r.runCtx.Muted())
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	event.DeployInProgress()
    52  	ctx, endTrace := instrumentation.StartTrace(ctx, "applyResources_Deploying")
    53  	defer endTrace()
    54  	r.deployer.RegisterLocalImages(localImages)
    55  	err = r.deployer.Deploy(ctx, deployOut, artifacts)
    56  	r.hasDeployed = true // set even if deploy may have failed, because we want to cleanup any partially created resources
    57  	postDeployFn()
    58  	if err != nil {
    59  		event.DeployFailed(err)
    60  		endTrace(instrumentation.TraceEndError(err))
    61  		return err
    62  	}
    63  	event.DeployComplete()
    64  	return nil
    65  }