Skip to content

Commit c504992

Browse files
authored
Revert_to_job (#281)
* reverted back to network-tester job
1 parent 34c5954 commit c504992

File tree

6 files changed

+69
-33
lines changed

6 files changed

+69
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
# Changelog:
22

3-
* Create git integration and user PAT even when installation check times out
4-
* Replace pre-install network check Job with a simple Pod
3+
* Reverted pre-install network check to Job

Makefile

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

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

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.256/cf-linux-amd64.tar.gz | tar zx
26+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.257/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.256/cf-darwin-amd64.tar.gz | tar zx
39+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.257/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.256
8+
version: 0.0.257
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

pkg/util/kube/kube.go

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/argoproj-labs/argocd-autopilot/pkg/kube"
2525
"github.com/codefresh-io/cli-v2/pkg/store"
2626
authv1 "k8s.io/api/authorization/v1"
27+
batchv1 "k8s.io/api/batch/v1"
2728
v1 "k8s.io/api/core/v1"
2829
"k8s.io/apimachinery/pkg/api/resource"
2930
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -54,13 +55,6 @@ type (
5455
RestartPolicy v1.RestartPolicy
5556
BackOffLimit int32
5657
}
57-
58-
RunPodOptions struct {
59-
Namespace string
60-
GenerateName string
61-
Image string
62-
Env []v1.EnvVar
63-
}
6458
)
6559

6660
func EnsureClusterRequirements(ctx context.Context, kubeFactory kube.Factory, namespace string) error {
@@ -235,24 +229,61 @@ func testNode(n v1.Node, req validationRequest) []string {
235229
return result
236230
}
237231

238-
func RunPod(ctx context.Context, client kubernetes.Interface, opts RunPodOptions) (*v1.Pod, error) {
239-
podSpec := &v1.Pod{
232+
func LaunchJob(ctx context.Context, client kubernetes.Interface, opts LaunchJobOptions) (*batchv1.Job, error) {
233+
jobSpec := &batchv1.Job{
240234
ObjectMeta: metav1.ObjectMeta{
241-
Namespace: opts.Namespace,
242-
GenerateName: opts.GenerateName,
235+
Name: *opts.JobName,
236+
Namespace: opts.Namespace,
243237
},
244-
Spec: v1.PodSpec{
245-
Containers: []v1.Container{
246-
{
247-
Name: opts.GenerateName,
248-
Image: opts.Image,
249-
Env: opts.Env,
238+
Spec: batchv1.JobSpec{
239+
Template: v1.PodTemplateSpec{
240+
Spec: v1.PodSpec{
241+
Containers: []v1.Container{
242+
{
243+
Name: *opts.JobName,
244+
Image: *opts.Image,
245+
Env: opts.Env,
246+
},
247+
},
248+
RestartPolicy: opts.RestartPolicy,
250249
},
251250
},
251+
BackoffLimit: &opts.BackOffLimit,
252252
},
253253
}
254254

255-
return client.CoreV1().Pods(opts.Namespace).Create(ctx, podSpec, metav1.CreateOptions{})
255+
return client.BatchV1().Jobs(opts.Namespace).Create(ctx, jobSpec, metav1.CreateOptions{})
256+
}
257+
258+
func DeleteJob(ctx context.Context, client kubernetes.Interface, job *batchv1.Job) error {
259+
err := client.BatchV1().Jobs(job.Namespace).Delete(ctx, job.Name, metav1.DeleteOptions{})
260+
if err != nil {
261+
return fmt.Errorf("fail to delete job resource \"%s\": %s", job.Name, err.Error())
262+
}
263+
264+
err = client.CoreV1().Pods(job.Namespace).DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{
265+
LabelSelector: "controller-uid=" + job.GetLabels()["controller-uid"],
266+
})
267+
if err != nil {
268+
return fmt.Errorf("fail to delete tester pod: %s", err.Error())
269+
}
270+
271+
return nil
272+
}
273+
274+
func GetPodByJob(ctx context.Context, client kubernetes.Interface, job *batchv1.Job) (*v1.Pod, error) {
275+
pods, err := client.CoreV1().Pods(store.Get().DefaultNamespace).List(ctx, metav1.ListOptions{
276+
LabelSelector: "controller-uid=" + job.GetLabels()["controller-uid"],
277+
})
278+
if err != nil {
279+
return nil, err
280+
}
281+
282+
if len(pods.Items) == 0 {
283+
return nil, nil
284+
}
285+
286+
return &pods.Items[0], nil
256287
}
257288

258289
func GetPodLogs(ctx context.Context, client kubernetes.Interface, namespace, name string) (string, error) {

pkg/util/util.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/codefresh-io/cli-v2/pkg/reporter"
3131
"github.com/codefresh-io/cli-v2/pkg/store"
3232
kubeutil "github.com/codefresh-io/cli-v2/pkg/util/kube"
33-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3433

3534
v1 "k8s.io/api/core/v1"
3635
"k8s.io/client-go/kubernetes"
@@ -212,24 +211,26 @@ func RunNetworkTest(ctx context.Context, kubeFactory kube.Factory, urls ...strin
212211
return fmt.Errorf("failed to create kubernetes client: %w", err)
213212
}
214213

215-
pod, err := kubeutil.RunPod(ctx, client, kubeutil.RunPodOptions{
216-
Namespace: store.Get().DefaultNamespace,
217-
GenerateName: store.Get().NetworkTesterName,
218-
Image: store.Get().NetworkTesterImage,
219-
Env: env,
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,
220221
})
221222
if err != nil {
222223
return err
223224
}
225+
224226
defer func() {
225-
err = client.CoreV1().Pods(store.Get().DefaultNamespace).Delete(ctx, pod.Name, metav1.DeleteOptions{})
227+
err := kubeutil.DeleteJob(ctx, client, job)
226228
if err != nil {
227229
log.G(ctx).Errorf("fail to delete tester pod: %s", err.Error())
228230
}
229231
}()
230232

231233
log.G(ctx).Info("Running network test...")
232-
233234
ticker := time.NewTicker(5 * time.Second)
234235
defer ticker.Stop()
235236
var podLastState *v1.Pod
@@ -240,11 +241,16 @@ Loop:
240241
select {
241242
case <-ticker.C:
242243
log.G(ctx).Debug("Waiting for network tester to finish")
243-
currentPod, err := client.CoreV1().Pods(store.Get().DefaultNamespace).Get(ctx, pod.Name, metav1.GetOptions{})
244+
currentPod, err := kubeutil.GetPodByJob(ctx, client, job)
244245
if err != nil {
245246
return err
246247
}
247248

249+
if currentPod == nil {
250+
log.G(ctx).Debug("Network tester pod: waiting for pod")
251+
continue
252+
}
253+
248254
if len(currentPod.Status.ContainerStatuses) == 0 {
249255
log.G(ctx).Debug("Network tester pod: creating container")
250256
continue

0 commit comments

Comments
 (0)