Skip to content

Commit 68d74b2

Browse files
committed
fix(kube): use k8s-compatible yaml for manifest apply/build
1 parent fe459de commit 68d74b2

3 files changed

Lines changed: 32 additions & 36 deletions

File tree

internal/kube/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"time"
1818

1919
"github.com/acmore/okdev/internal/shellutil"
20-
"gopkg.in/yaml.v3"
2120
coordinationv1 "k8s.io/api/coordination/v1"
2221
corev1 "k8s.io/api/core/v1"
2322
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -31,6 +30,7 @@ import (
3130
"k8s.io/client-go/tools/portforward"
3231
"k8s.io/client-go/tools/remotecommand"
3332
"k8s.io/client-go/transport/spdy"
33+
"sigs.k8s.io/yaml"
3434
)
3535

3636
type Client struct {

internal/kube/manifests.go

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import (
44
"fmt"
55

66
"github.com/acmore/okdev/internal/config"
7-
"gopkg.in/yaml.v3"
87
corev1 "k8s.io/api/core/v1"
98
"k8s.io/apimachinery/pkg/api/resource"
10-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"sigs.k8s.io/yaml"
1110
)
1211

1312
func BuildPVCManifest(namespace, name, size, storageClass string, labels map[string]string, annotations map[string]string) ([]byte, error) {
@@ -18,52 +17,49 @@ func BuildPVCManifest(namespace, name, size, storageClass string, labels map[str
1817
if err != nil {
1918
return nil, fmt.Errorf("parse pvc size %q: %w", size, err)
2019
}
21-
pvc := corev1.PersistentVolumeClaim{
22-
TypeMeta: metav1.TypeMeta{
23-
APIVersion: "v1",
24-
Kind: "PersistentVolumeClaim",
25-
},
26-
ObjectMeta: metav1.ObjectMeta{
27-
Name: name,
28-
Namespace: namespace,
29-
Labels: labels,
30-
Annotations: annotations,
31-
},
32-
Spec: corev1.PersistentVolumeClaimSpec{
33-
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
34-
Resources: corev1.VolumeResourceRequirements{
35-
Requests: corev1.ResourceList{
36-
corev1.ResourceStorage: qty,
37-
},
20+
pvcSpec := corev1.PersistentVolumeClaimSpec{
21+
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
22+
Resources: corev1.VolumeResourceRequirements{
23+
Requests: corev1.ResourceList{
24+
corev1.ResourceStorage: qty,
3825
},
3926
},
4027
}
4128
if storageClass != "" {
42-
pvc.Spec.StorageClassName = &storageClass
29+
pvcSpec.StorageClassName = &storageClass
4330
}
44-
b, err := yaml.Marshal(pvc)
31+
manifest := map[string]any{
32+
"apiVersion": "v1",
33+
"kind": "PersistentVolumeClaim",
34+
"metadata": map[string]any{
35+
"name": name,
36+
"namespace": namespace,
37+
"labels": labels,
38+
"annotations": annotations,
39+
},
40+
"spec": pvcSpec,
41+
}
42+
b, err := yaml.Marshal(manifest)
4543
if err != nil {
4644
return nil, fmt.Errorf("marshal pvc manifest: %w", err)
4745
}
4846
return b, nil
4947
}
5048

5149
func BuildPodManifest(namespace, name string, labels map[string]string, annotations map[string]string, podSpec corev1.PodSpec) ([]byte, error) {
52-
pod := corev1.Pod{
53-
TypeMeta: metav1.TypeMeta{
54-
APIVersion: "v1",
55-
Kind: "Pod",
56-
},
57-
ObjectMeta: metav1.ObjectMeta{
58-
Name: name,
59-
Namespace: namespace,
60-
Labels: labels,
61-
Annotations: annotations,
50+
manifest := map[string]any{
51+
"apiVersion": "v1",
52+
"kind": "Pod",
53+
"metadata": map[string]any{
54+
"name": name,
55+
"namespace": namespace,
56+
"labels": labels,
57+
"annotations": annotations,
6258
},
63-
Spec: podSpec,
59+
"spec": podSpec,
6460
}
6561

66-
b, err := yaml.Marshal(pod)
62+
b, err := yaml.Marshal(manifest)
6763
if err != nil {
6864
return nil, fmt.Errorf("marshal pod manifest: %w", err)
6965
}

internal/kube/manifests_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestBuildPVCManifest(t *testing.T) {
1313
t.Fatal(err)
1414
}
1515
s := string(m)
16-
if !strings.Contains(s, "PersistentVolumeClaim") || !strings.Contains(s, "ReadWriteOnce") || !strings.Contains(s, "okdev.io/ttl-hours") {
16+
if !strings.Contains(s, "kind: PersistentVolumeClaim") || !strings.Contains(s, "apiVersion: v1") || !strings.Contains(s, "ReadWriteOnce") || !strings.Contains(s, "okdev.io/ttl-hours") {
1717
t.Fatalf("unexpected manifest: %s", s)
1818
}
1919
}
@@ -27,7 +27,7 @@ func TestBuildPodManifestDefault(t *testing.T) {
2727
t.Fatal(err)
2828
}
2929
s := string(m)
30-
if !strings.Contains(s, "kind: Pod") || !strings.Contains(s, "name: dev") {
30+
if !strings.Contains(s, "kind: Pod") || !strings.Contains(s, "apiVersion: v1") || !strings.Contains(s, "name: pod1") {
3131
t.Fatalf("unexpected pod manifest: %s", s)
3232
}
3333
}

0 commit comments

Comments
 (0)