Skip to content

Commit aee4b86

Browse files
authored
feat(get): add code to get resource (#176)
Signed-off-by: Amit Bhatt <[email protected]>
1 parent 941011d commit aee4b86

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed

pkg/recipe/get.go

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
Copyright 2020 The MayaData 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+
https://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 recipe
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/pkg/errors"
23+
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
26+
types "mayadata.io/d-operators/types/recipe"
27+
)
28+
29+
// Gettable helps fetching desired state from the cluster
30+
type Gettable struct {
31+
BaseRunner
32+
Get *types.Get
33+
34+
result *types.GetResult
35+
err error
36+
}
37+
38+
// GettableConfig helps in creating new instance of Gettable
39+
type GettableConfig struct {
40+
BaseRunner
41+
Get *types.Get
42+
}
43+
44+
// NewGetter returns a new instance of Gettable
45+
func NewGetter(config GettableConfig) *Gettable {
46+
return &Gettable{
47+
BaseRunner: config.BaseRunner,
48+
Get: config.Get,
49+
result: &types.GetResult{},
50+
}
51+
}
52+
53+
func (g *Gettable) getCRD() (*types.GetResult, error) {
54+
var crd *v1beta1.CustomResourceDefinition
55+
err := UnstructToTyped(g.Get.State, &crd)
56+
if err != nil {
57+
return nil, errors.Wrapf(
58+
err,
59+
"Failed to transform unstruct instance to crd equivalent",
60+
)
61+
}
62+
// use crd client to get crds
63+
obj, err := g.crdClient.
64+
CustomResourceDefinitions().
65+
Get(g.Get.State.GetName(), metav1.GetOptions{})
66+
if err != nil {
67+
return nil, errors.Wrapf(
68+
err,
69+
"Failed to get crd %q",
70+
g.Get.State.GetName(),
71+
)
72+
}
73+
return &types.GetResult{
74+
Phase: types.GetStatusPassed,
75+
Message: fmt.Sprintf(
76+
"Get CRD: Kind %s: APIVersion %s: Name %s",
77+
crd.Spec.Names.Singular,
78+
crd.Spec.Group+"/"+crd.Spec.Version,
79+
g.Get.State.GetName(),
80+
),
81+
V1Beta1CRD: obj,
82+
}, nil
83+
}
84+
85+
func (g *Gettable) getResource() (*types.GetResult, error) {
86+
var message = fmt.Sprintf(
87+
"Get resource with %s / %s: GVK %s",
88+
g.Get.State.GetNamespace(),
89+
g.Get.State.GetName(),
90+
g.Get.State.GroupVersionKind(),
91+
)
92+
client, err := g.GetClientForAPIVersionAndKind(
93+
g.Get.State.GetAPIVersion(),
94+
g.Get.State.GetKind(),
95+
)
96+
if err != nil {
97+
return nil, errors.Wrapf(
98+
err,
99+
"Failed to get resource client",
100+
)
101+
}
102+
obj, err := client.
103+
Namespace(g.Get.State.GetNamespace()).
104+
Get(g.Get.State.GetName(), metav1.GetOptions{})
105+
if err != nil {
106+
return nil, errors.Wrapf(
107+
err,
108+
"Failed to get resource",
109+
)
110+
}
111+
return &types.GetResult{
112+
Phase: types.GetStatusPassed,
113+
Message: message,
114+
Object: obj,
115+
}, nil
116+
}
117+
118+
// Run executes applying the desired state against the
119+
// cluster
120+
func (g *Gettable) Run() (*types.GetResult, error) {
121+
if g.Get.State.GetKind() == "CustomResourceDefinition" {
122+
// get CRD
123+
return g.getCRD()
124+
}
125+
return g.getResource()
126+
}

types/recipe/get.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright 2020 The MayaData 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+
https://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 types
18+
19+
import (
20+
"encoding/json"
21+
22+
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
23+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24+
)
25+
26+
// Get represents the desired state that needs to
27+
// be fetched from the cluster
28+
type Get struct {
29+
// Desired state that needs to be fetched from the
30+
// Kubernetes cluster
31+
State *unstructured.Unstructured `json:"state"`
32+
}
33+
34+
// String implements the Stringer interface
35+
func (g Get) String() string {
36+
raw, err := json.MarshalIndent(
37+
g,
38+
" ",
39+
".",
40+
)
41+
if err != nil {
42+
panic(err)
43+
}
44+
return string(raw)
45+
}
46+
47+
// GetStatusPhase is a typed definition to determine the
48+
// result of executing a get invocation
49+
type GetStatusPhase string
50+
51+
const (
52+
// GetStatusPassed defines a successful get
53+
GetStatusPassed GetStatusPhase = "Passed"
54+
55+
// GetStatusWarning defines a get that resulted in warnings
56+
GetStatusWarning GetStatusPhase = "Warning"
57+
58+
// GetStatusFailed defines a failed get
59+
GetStatusFailed GetStatusPhase = "Failed"
60+
)
61+
62+
// ToTaskStatusPhase transforms GetStatusPhase to TaskStatusPhase
63+
func (phase GetStatusPhase) ToTaskStatusPhase() TaskStatusPhase {
64+
switch phase {
65+
case GetStatusPassed:
66+
return TaskStatusPassed
67+
case GetStatusFailed:
68+
return TaskStatusFailed
69+
case GetStatusWarning:
70+
return TaskStatusWarning
71+
default:
72+
return ""
73+
}
74+
}
75+
76+
// GetResult holds the result of the get operation
77+
type GetResult struct {
78+
Phase GetStatusPhase `json:"phase"`
79+
Message string `json:"message,omitempty"`
80+
Verbose string `json:"verbose,omitempty"`
81+
Warning string `json:"warning,omitempty"`
82+
V1Beta1CRD *v1beta1.CustomResourceDefinition `json:"v1b1CRD,omitempty"`
83+
Object *unstructured.Unstructured `json:"object,omitempty"`
84+
}

0 commit comments

Comments
 (0)