|
| 1 | +/* |
| 2 | +Copyright 2019 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 deploy |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "strings" |
| 23 | + "sync" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/kubectl" |
| 27 | + kubernetesutil "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes" |
| 28 | + runcontext "github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/context" |
| 29 | + "github.com/pkg/errors" |
| 30 | + "github.com/sirupsen/logrus" |
| 31 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 | + "k8s.io/client-go/kubernetes" |
| 33 | +) |
| 34 | + |
| 35 | +var ( |
| 36 | + // TODO: Move this to a flag or global config. |
| 37 | + // Default deadline set to 10 minutes. This is default value for progressDeadlineInSeconds |
| 38 | + // See: https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/api/apps/v1/types.go#L305 |
| 39 | + defaultStatusCheckDeadlineInSeconds int32 = 600 |
| 40 | + // Poll period for checking set to 100 milliseconds |
| 41 | + defaultPollPeriodInMilliseconds = 100 |
| 42 | + |
| 43 | + // For testing |
| 44 | + executeRolloutStatus = getRollOutStatus |
| 45 | +) |
| 46 | + |
| 47 | +func StatusCheck(ctx context.Context, defaultLabeller *DefaultLabeller, runCtx *runcontext.RunContext) error { |
| 48 | + |
| 49 | + client, err := kubernetesutil.GetClientset() |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + dMap, err := getDeployments(client, runCtx.Opts.Namespace, defaultLabeller) |
| 54 | + if err != nil { |
| 55 | + return errors.Wrap(err, "could not fetch deployments") |
| 56 | + } |
| 57 | + |
| 58 | + wg := sync.WaitGroup{} |
| 59 | + // Its safe to use sync.Map without locks here as each subroutine adds a different key to the map. |
| 60 | + syncMap := &sync.Map{} |
| 61 | + kubeCtl := &kubectl.CLI{ |
| 62 | + Namespace: runCtx.Opts.Namespace, |
| 63 | + KubeContext: runCtx.KubeContext, |
| 64 | + } |
| 65 | + |
| 66 | + for dName, deadline := range dMap { |
| 67 | + deadlineDuration := time.Duration(deadline) * time.Second |
| 68 | + wg.Add(1) |
| 69 | + go func(dName string, deadlineDuration time.Duration) { |
| 70 | + defer wg.Done() |
| 71 | + pollDeploymentRolloutStatus(ctx, kubeCtl, dName, deadlineDuration, syncMap) |
| 72 | + }(dName, deadlineDuration) |
| 73 | + } |
| 74 | + |
| 75 | + // Wait for all deployment status to be fetched |
| 76 | + wg.Wait() |
| 77 | + return getSkaffoldDeployStatus(syncMap) |
| 78 | +} |
| 79 | + |
| 80 | +func getDeployments(client kubernetes.Interface, ns string, l *DefaultLabeller) (map[string]int32, error) { |
| 81 | + |
| 82 | + deps, err := client.AppsV1().Deployments(ns).List(metav1.ListOptions{ |
| 83 | + LabelSelector: l.K8sManagedByLabelKeyValueString(), |
| 84 | + }) |
| 85 | + if err != nil { |
| 86 | + return nil, errors.Wrap(err, "could not fetch deployments") |
| 87 | + } |
| 88 | + |
| 89 | + depMap := map[string]int32{} |
| 90 | + |
| 91 | + for _, d := range deps.Items { |
| 92 | + var deadline int32 |
| 93 | + if d.Spec.ProgressDeadlineSeconds == nil { |
| 94 | + logrus.Debugf("no progressDeadlineSeconds config found for deployment %s. Setting deadline to %d seconds", d.Name, defaultStatusCheckDeadlineInSeconds) |
| 95 | + deadline = defaultStatusCheckDeadlineInSeconds |
| 96 | + } else { |
| 97 | + deadline = *d.Spec.ProgressDeadlineSeconds |
| 98 | + } |
| 99 | + depMap[d.Name] = deadline |
| 100 | + } |
| 101 | + |
| 102 | + return depMap, nil |
| 103 | +} |
| 104 | + |
| 105 | +func pollDeploymentRolloutStatus(ctx context.Context, k *kubectl.CLI, dName string, deadline time.Duration, syncMap *sync.Map) { |
| 106 | + pollDuration := time.Duration(defaultPollPeriodInMilliseconds) * time.Millisecond |
| 107 | + // Add poll duration to account for one last attempt after progressDeadlineSeconds. |
| 108 | + timeoutContext, cancel := context.WithTimeout(ctx, deadline+pollDuration) |
| 109 | + logrus.Debugf("checking rollout status %s", dName) |
| 110 | + defer cancel() |
| 111 | + for { |
| 112 | + select { |
| 113 | + case <-timeoutContext.Done(): |
| 114 | + syncMap.Store(dName, errors.Wrap(timeoutContext.Err(), fmt.Sprintf("deployment rollout status could not be fetched within %v", deadline))) |
| 115 | + return |
| 116 | + case <-time.After(pollDuration): |
| 117 | + status, err := executeRolloutStatus(timeoutContext, k, dName) |
| 118 | + if err != nil { |
| 119 | + syncMap.Store(dName, err) |
| 120 | + return |
| 121 | + } |
| 122 | + if strings.Contains(status, "successfully rolled out") { |
| 123 | + syncMap.Store(dName, nil) |
| 124 | + return |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func getSkaffoldDeployStatus(m *sync.Map) error { |
| 131 | + errorStrings := []string{} |
| 132 | + m.Range(func(k, v interface{}) bool { |
| 133 | + if t, ok := v.(error); ok { |
| 134 | + errorStrings = append(errorStrings, fmt.Sprintf("deployment %s failed due to %s", k, t.Error())) |
| 135 | + } |
| 136 | + return true |
| 137 | + }) |
| 138 | + |
| 139 | + if len(errorStrings) == 0 { |
| 140 | + return nil |
| 141 | + } |
| 142 | + return fmt.Errorf("following deployments are not stable:\n%s", strings.Join(errorStrings, "\n")) |
| 143 | +} |
| 144 | + |
| 145 | +func getRollOutStatus(ctx context.Context, k *kubectl.CLI, dName string) (string, error) { |
| 146 | + b, err := k.RunOut(ctx, nil, "rollout", []string{"status", "deployment", dName}, |
| 147 | + "--watch=false") |
| 148 | + return string(b), err |
| 149 | +} |
0 commit comments