/
appresource.go
103 lines (88 loc) · 2.29 KB
/
appresource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2020 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package testutil
import (
"context"
"fmt"
"io"
"os"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/apimachinery/pkg/util/yaml"
applicationsv1beta1 "sigs.k8s.io/application/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// CreateApplication - create application object
func CreateApplication(kubeClient client.Client, ns string, relativePath string) error {
app, err := parseApplicationYaml(relativePath)
if err != nil {
return err
}
app.Namespace = ns
object := &applicationsv1beta1.Application{}
objectKey := types.NamespacedName{
Namespace: ns,
Name: app.Name,
}
err = kubeClient.Get(context.TODO(), objectKey, object)
if err == nil {
// Application already exists -> Update
err = kubeClient.Update(context.TODO(), app)
if err != nil {
return err
}
} else {
// Application doesn't exist -> Create
fmt.Printf("Creating new Application\n")
err = kubeClient.Create(context.TODO(), app)
if err != nil {
return err
}
}
return nil
}
// DeleteApplication - delete application object
func DeleteApplication(kubeClient client.Client, ns string, relativePath string) error {
app, err := parseApplicationYaml(relativePath)
if err != nil {
return err
}
object := &applicationsv1beta1.Application{}
objectKey := types.NamespacedName{
Namespace: ns,
Name: app.Name,
}
err = kubeClient.Get(context.TODO(), objectKey, object)
if err != nil {
return err
}
return kubeClient.Delete(context.TODO(), object)
}
func parseApplicationYaml(relativePath string) (*applicationsv1beta1.Application, error) {
var manifest *os.File
var err error
var app applicationsv1beta1.Application
if manifest, err = PathToOSFile(relativePath); err != nil {
return nil, err
}
decoder := yaml.NewYAMLOrJSONDecoder(manifest, 100)
for {
var out unstructured.Unstructured
err = decoder.Decode(&out)
if err != nil {
// this would indicate it's malformed YAML.
break
}
if out.GetKind() == "Application" {
var marshaled []byte
marshaled, err = out.MarshalJSON()
_ = json.Unmarshal(marshaled, &app)
break
}
}
if err != io.EOF && err != nil {
return nil, err
}
return &app, nil
}