Skip to content

Commit bdc300d

Browse files
version check precedence (#267)
* moved network test to the end of the cluster requirements * bump * network test outside of cluster requirement checks with report * small fix * small fixes * bump * removed report for network test, it is now united with cluster requirements * remove empty lines * removed empty line * bump * bump * bump * solve merge conflicts * more merge solving * bump * some errors descriptions * resolve conflicts * resolve conflicts * bump
1 parent c504992 commit bdc300d

File tree

7 files changed

+135
-137
lines changed

7 files changed

+135
-137
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.0.257
1+
VERSION=v0.0.258
22

33
OUT_DIR=dist
44
YEAR?=$(shell date +"%Y")

cmd/commands/runtime.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -908,18 +908,11 @@ func preInstallationChecks(ctx context.Context, opts *RuntimeInstallOptions) err
908908
}
909909

910910
if !opts.SkipClusterChecks {
911-
err = util.RunNetworkTest(ctx, opts.KubeFactory, cfConfig.GetCurrentContext().URL)
912-
handleCliStep(reporter.InstallStepRunPreCheckClusterChecks, "Running cluster checks", err, false)
913-
if err != nil {
914-
return fmt.Errorf(fmt.Sprintf("cluster network tests failed: %v ", err))
915-
}
916-
917-
log.G(ctx).Info("Network test finished successfully")
918-
err = kubeutil.EnsureClusterRequirements(ctx, opts.KubeFactory, opts.RuntimeName)
919-
handleCliStep(reporter.InstallStepRunPreCheckValidateClusterRequirements, "Ensuring cluster requirements", err, false)
920-
if err != nil {
921-
return fmt.Errorf(fmt.Sprintf("validation of minimum cluster requirements failed: %v ", err))
922-
}
911+
err = kubeutil.EnsureClusterRequirements(ctx, opts.KubeFactory, opts.RuntimeName, cfConfig.GetCurrentContext().URL)
912+
}
913+
handleCliStep(reporter.InstallStepRunPreCheckValidateClusterRequirements, "Ensuring cluster requirements", err, false)
914+
if err != nil {
915+
return fmt.Errorf("validation of minimum cluster requirements failed: %w", err)
923916
}
924917

925918
return nil

docs/releases/release_notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cf version
2323

2424
```bash
2525
# download and extract the binary
26-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.257/cf-linux-amd64.tar.gz | tar zx
26+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.258/cf-linux-amd64.tar.gz | tar zx
2727

2828
# move the binary to your $PATH
2929
mv ./cf-linux-amd64 /usr/local/bin/cf
@@ -36,7 +36,7 @@ cf version
3636

3737
```bash
3838
# download and extract the binary
39-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.257/cf-darwin-amd64.tar.gz | tar zx
39+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.258/cf-darwin-amd64.tar.gz | tar zx
4040

4141
# move the binary to your $PATH
4242
mv ./cf-darwin-amd64 /usr/local/bin/cf

manifests/runtime.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
namespace: "{{ namespace }}"
66
spec:
77
defVersion: 1.0.1
8-
version: 0.0.257
8+
version: 0.0.258
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

pkg/reporter/reporter.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ const (
7575
InstallStepRunPreCheckEnsureCliVersion CliStep = "install.run.pre-check.step.ensure-cli-version"
7676
InstallStepRunPreCheckRuntimeCollision CliStep = "install.run.pre-check.step.runtime-collision"
7777
InstallStepRunPreCheckExisitingRuntimes CliStep = "install.run.pre-check.step.existing-runtimes"
78-
InstallStepRunPreCheckClusterChecks CliStep = "install.run.pre-check.step.cluster-checks"
7978
InstallStepRunPreCheckValidateClusterRequirements CliStep = "install.run.pre-check.step.validate-cluster-requirements"
8079
InstallPhaseRunPreCheckFinish CliStep = "install.run.pre-check.phase.finish"
8180
InstallPhaseStart CliStep = "install.run.phase.start"

pkg/util/kube/kube.go

Lines changed: 126 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import (
2020
"fmt"
2121
"io"
2222
"strings"
23+
"time"
2324

2425
"github.com/argoproj-labs/argocd-autopilot/pkg/kube"
26+
"github.com/codefresh-io/cli-v2/pkg/log"
2527
"github.com/codefresh-io/cli-v2/pkg/store"
2628
authv1 "k8s.io/api/authorization/v1"
2729
batchv1 "k8s.io/api/batch/v1"
@@ -57,7 +59,7 @@ type (
5759
}
5860
)
5961

60-
func EnsureClusterRequirements(ctx context.Context, kubeFactory kube.Factory, namespace string) error {
62+
func EnsureClusterRequirements(ctx context.Context, kubeFactory kube.Factory, namespace string, contextUrl string) error {
6163
requirementsValidationErrorMessage := "cluster does not meet minimum requirements"
6264
var specificErrorMessages []string
6365

@@ -169,6 +171,123 @@ func EnsureClusterRequirements(ctx context.Context, kubeFactory kube.Factory, na
169171
return fmt.Errorf("%s: %v", requirementsValidationErrorMessage, specificErrorMessages)
170172
}
171173

174+
err = runNetworkTest(ctx, kubeFactory, contextUrl)
175+
if err != nil {
176+
return fmt.Errorf("cluster network tests failed: %w ", err)
177+
}
178+
179+
log.G(ctx).Info("Network test finished successfully")
180+
181+
return nil
182+
}
183+
184+
func runNetworkTest(ctx context.Context, kubeFactory kube.Factory, urls ...string) error {
185+
const networkTestsTimeout = 120 * time.Second
186+
187+
envVars := map[string]string{
188+
"URLS": strings.Join(urls, ","),
189+
"IN_CLUSTER": "1",
190+
}
191+
env := prepareEnvVars(envVars)
192+
193+
client, err := kubeFactory.KubernetesClientSet()
194+
if err != nil {
195+
return fmt.Errorf("failed to create kubernetes client: %w", err)
196+
}
197+
198+
job, err := launchJob(ctx, client, LaunchJobOptions{
199+
Namespace: store.Get().DefaultNamespace,
200+
JobName: &store.Get().NetworkTesterName,
201+
Image: &store.Get().NetworkTesterImage,
202+
Env: env,
203+
RestartPolicy: v1.RestartPolicyNever,
204+
BackOffLimit: 0,
205+
})
206+
if err != nil {
207+
return err
208+
}
209+
210+
defer func() {
211+
err := deleteJob(ctx, client, job)
212+
if err != nil {
213+
log.G(ctx).Errorf("fail to delete tester pod: %s", err.Error())
214+
}
215+
}()
216+
217+
log.G(ctx).Info("Running network test...")
218+
ticker := time.NewTicker(5 * time.Second)
219+
defer ticker.Stop()
220+
var podLastState *v1.Pod
221+
timeoutChan := time.After(networkTestsTimeout)
222+
223+
Loop:
224+
for {
225+
select {
226+
case <-ticker.C:
227+
log.G(ctx).Debug("Waiting for network tester to finish")
228+
currentPod, err := getPodByJob(ctx, client, job)
229+
if err != nil {
230+
return err
231+
}
232+
233+
if currentPod == nil {
234+
log.G(ctx).Debug("Network tester pod: waiting for pod")
235+
continue
236+
}
237+
238+
if len(currentPod.Status.ContainerStatuses) == 0 {
239+
log.G(ctx).Debug("Network tester pod: creating container")
240+
continue
241+
}
242+
243+
state := currentPod.Status.ContainerStatuses[0].State
244+
if state.Running != nil {
245+
log.G(ctx).Debug("Network tester pod: running")
246+
}
247+
248+
if state.Waiting != nil {
249+
log.G(ctx).Debug("Network tester pod: waiting")
250+
}
251+
252+
if state.Terminated != nil {
253+
log.G(ctx).Debug("Network tester pod: terminated")
254+
podLastState = currentPod
255+
break Loop
256+
}
257+
case <-timeoutChan:
258+
return fmt.Errorf("network test timeout reached!")
259+
}
260+
}
261+
262+
return checkPodLastState(ctx, client, podLastState)
263+
}
264+
265+
func prepareEnvVars(vars map[string]string) []v1.EnvVar {
266+
var env []v1.EnvVar
267+
for key, value := range vars {
268+
env = append(env, v1.EnvVar{
269+
Name: key,
270+
Value: value,
271+
})
272+
}
273+
274+
return env
275+
}
276+
277+
func checkPodLastState(ctx context.Context, client kubernetes.Interface, pod *v1.Pod) error {
278+
terminated := pod.Status.ContainerStatuses[0].State.Terminated
279+
if terminated.ExitCode != 0 {
280+
logs, err := getPodLogs(ctx, client, pod.Namespace, pod.Name)
281+
if err != nil {
282+
log.G(ctx).Errorf("Failed getting logs from network-tester pod: $s", err.Error())
283+
} else {
284+
log.G(ctx).Error(logs)
285+
}
286+
287+
terminationMessage := strings.Trim(terminated.Message, "\n")
288+
return fmt.Errorf("Network test failed with: %s", terminationMessage)
289+
}
290+
172291
return nil
173292
}
174293

@@ -229,7 +348,7 @@ func testNode(n v1.Node, req validationRequest) []string {
229348
return result
230349
}
231350

232-
func LaunchJob(ctx context.Context, client kubernetes.Interface, opts LaunchJobOptions) (*batchv1.Job, error) {
351+
func launchJob(ctx context.Context, client kubernetes.Interface, opts LaunchJobOptions) (*batchv1.Job, error) {
233352
jobSpec := &batchv1.Job{
234353
ObjectMeta: metav1.ObjectMeta{
235354
Name: *opts.JobName,
@@ -255,7 +374,7 @@ func LaunchJob(ctx context.Context, client kubernetes.Interface, opts LaunchJobO
255374
return client.BatchV1().Jobs(opts.Namespace).Create(ctx, jobSpec, metav1.CreateOptions{})
256375
}
257376

258-
func DeleteJob(ctx context.Context, client kubernetes.Interface, job *batchv1.Job) error {
377+
func deleteJob(ctx context.Context, client kubernetes.Interface, job *batchv1.Job) error {
259378
err := client.BatchV1().Jobs(job.Namespace).Delete(ctx, job.Name, metav1.DeleteOptions{})
260379
if err != nil {
261380
return fmt.Errorf("fail to delete job resource \"%s\": %s", job.Name, err.Error())
@@ -271,7 +390,7 @@ func DeleteJob(ctx context.Context, client kubernetes.Interface, job *batchv1.Jo
271390
return nil
272391
}
273392

274-
func GetPodByJob(ctx context.Context, client kubernetes.Interface, job *batchv1.Job) (*v1.Pod, error) {
393+
func getPodByJob(ctx context.Context, client kubernetes.Interface, job *batchv1.Job) (*v1.Pod, error) {
275394
pods, err := client.CoreV1().Pods(store.Get().DefaultNamespace).List(ctx, metav1.ListOptions{
276395
LabelSelector: "controller-uid=" + job.GetLabels()["controller-uid"],
277396
})
@@ -286,18 +405,18 @@ func GetPodByJob(ctx context.Context, client kubernetes.Interface, job *batchv1.
286405
return &pods.Items[0], nil
287406
}
288407

289-
func GetPodLogs(ctx context.Context, client kubernetes.Interface, namespace, name string) (string, error) {
408+
func getPodLogs(ctx context.Context, client kubernetes.Interface, namespace, name string) (string, error) {
290409
req := client.CoreV1().Pods(namespace).GetLogs(name, &v1.PodLogOptions{})
291410
podLogs, err := req.Stream(ctx)
292411
if err != nil {
293-
return "", fmt.Errorf("Failed to get network-tester pod logs: %w", err)
412+
return "", fmt.Errorf("failed to get network-tester pod logs: %w", err)
294413
}
295414
defer podLogs.Close()
296415

297416
logsBuf := new(bytes.Buffer)
298417
_, err = io.Copy(logsBuf, podLogs)
299418
if err != nil {
300-
return "", fmt.Errorf("Failed to read network-tester pod logs: %w", err)
419+
return "", fmt.Errorf("failed to read network-tester pod logs: %w", err)
301420
}
302421

303422
return strings.Trim(logsBuf.String(), "\n"), nil

pkg/util/util.go

Lines changed: 0 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,11 @@ import (
2424
"sync"
2525
"time"
2626

27-
"github.com/argoproj-labs/argocd-autopilot/pkg/kube"
2827
"github.com/briandowns/spinner"
2928
"github.com/codefresh-io/cli-v2/pkg/log"
3029
"github.com/codefresh-io/cli-v2/pkg/reporter"
3130
"github.com/codefresh-io/cli-v2/pkg/store"
32-
kubeutil "github.com/codefresh-io/cli-v2/pkg/util/kube"
3331

34-
v1 "k8s.io/api/core/v1"
35-
"k8s.io/client-go/kubernetes"
3632
"k8s.io/client-go/tools/clientcmd"
3733
)
3834

@@ -197,112 +193,3 @@ func IsIP(s string) bool {
197193
return ipRegexp.MatchString(s)
198194
}
199195

200-
func RunNetworkTest(ctx context.Context, kubeFactory kube.Factory, urls ...string) error {
201-
const networkTestsTimeout = 120 * time.Second
202-
203-
envVars := map[string]string{
204-
"URLS": strings.Join(urls, ","),
205-
"IN_CLUSTER": "1",
206-
}
207-
env := prepareEnvVars(envVars)
208-
209-
client, err := kubeFactory.KubernetesClientSet()
210-
if err != nil {
211-
return fmt.Errorf("failed to create kubernetes client: %w", err)
212-
}
213-
214-
job, err := kubeutil.LaunchJob(ctx, client, kubeutil.LaunchJobOptions{
215-
Namespace: store.Get().DefaultNamespace,
216-
JobName: &store.Get().NetworkTesterName,
217-
Image: &store.Get().NetworkTesterImage,
218-
Env: env,
219-
RestartPolicy: v1.RestartPolicyNever,
220-
BackOffLimit: 0,
221-
})
222-
if err != nil {
223-
return err
224-
}
225-
226-
defer func() {
227-
err := kubeutil.DeleteJob(ctx, client, job)
228-
if err != nil {
229-
log.G(ctx).Errorf("fail to delete tester pod: %s", err.Error())
230-
}
231-
}()
232-
233-
log.G(ctx).Info("Running network test...")
234-
ticker := time.NewTicker(5 * time.Second)
235-
defer ticker.Stop()
236-
var podLastState *v1.Pod
237-
timeoutChan := time.After(networkTestsTimeout)
238-
239-
Loop:
240-
for {
241-
select {
242-
case <-ticker.C:
243-
log.G(ctx).Debug("Waiting for network tester to finish")
244-
currentPod, err := kubeutil.GetPodByJob(ctx, client, job)
245-
if err != nil {
246-
return err
247-
}
248-
249-
if currentPod == nil {
250-
log.G(ctx).Debug("Network tester pod: waiting for pod")
251-
continue
252-
}
253-
254-
if len(currentPod.Status.ContainerStatuses) == 0 {
255-
log.G(ctx).Debug("Network tester pod: creating container")
256-
continue
257-
}
258-
259-
state := currentPod.Status.ContainerStatuses[0].State
260-
if state.Running != nil {
261-
log.G(ctx).Debug("Network tester pod: running")
262-
}
263-
264-
if state.Waiting != nil {
265-
log.G(ctx).Debug("Network tester pod: waiting")
266-
}
267-
268-
if state.Terminated != nil {
269-
log.G(ctx).Debug("Network tester pod: terminated")
270-
podLastState = currentPod
271-
break Loop
272-
}
273-
case <-timeoutChan:
274-
return fmt.Errorf("network test timeout reached!")
275-
}
276-
}
277-
278-
return checkPodLastState(ctx, client, podLastState)
279-
}
280-
281-
func prepareEnvVars(vars map[string]string) []v1.EnvVar {
282-
var env []v1.EnvVar
283-
for key, value := range vars {
284-
env = append(env, v1.EnvVar{
285-
Name: key,
286-
Value: value,
287-
})
288-
}
289-
290-
return env
291-
}
292-
293-
func checkPodLastState(ctx context.Context, client kubernetes.Interface, pod *v1.Pod) error {
294-
terminated := pod.Status.ContainerStatuses[0].State.Terminated
295-
if terminated.ExitCode != 0 {
296-
logs, err := kubeutil.GetPodLogs(ctx, client, pod.Namespace, pod.Name)
297-
if err != nil {
298-
log.G(ctx).Errorf("Failed getting logs from network-tester pod: $s", err.Error())
299-
} else {
300-
log.G(ctx).Error(logs)
301-
}
302-
303-
terminationMessage := strings.Trim(terminated.Message, "\n")
304-
return fmt.Errorf("Network test failed with: %s", terminationMessage)
305-
}
306-
307-
return nil
308-
}

0 commit comments

Comments
 (0)