Skip to content

Commit de8c299

Browse files
author
Amit Kumar Das
authored
feat(list): add list option in recipe package (#173)
This commit allows one to list any type of Kubernetes resources including CRDs. This option is currently exposed to be consumed by packages other than recipe within d-operator as well as by libraries consuming d-operators. Signed-off-by: AmitKumarDas <[email protected]>
1 parent 32db107 commit de8c299

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed

pkg/recipe/list.go

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
25+
types "mayadata.io/d-operators/types/recipe"
26+
)
27+
28+
// Listable helps listing desired state from the cluster
29+
type Listable struct {
30+
BaseRunner
31+
List *types.List
32+
33+
result *types.ListResult
34+
err error
35+
}
36+
37+
// ListableConfig helps in creating new instance of Listable
38+
type ListableConfig struct {
39+
BaseRunner
40+
List *types.List
41+
}
42+
43+
// NewLister returns a new instance of Listable
44+
func NewLister(config ListableConfig) *Listable {
45+
return &Listable{
46+
BaseRunner: config.BaseRunner,
47+
List: config.List,
48+
result: &types.ListResult{},
49+
}
50+
}
51+
52+
func (l *Listable) listCRDs() (*types.ListResult, error) {
53+
var crd *v1beta1.CustomResourceDefinition
54+
err := UnstructToTyped(l.List.State, &crd)
55+
if err != nil {
56+
return nil, err
57+
}
58+
// use crd client to list crds
59+
items, err := l.crdClient.
60+
CustomResourceDefinitions().
61+
List(metav1.ListOptions{})
62+
if err != nil {
63+
return nil, err
64+
}
65+
return &types.ListResult{
66+
Phase: types.ListStatusPassed,
67+
Message: fmt.Sprintf(
68+
"List CRD: Kind %s: APIVersion %s",
69+
crd.Spec.Names.Singular,
70+
crd.Spec.Group+"/"+crd.Spec.Version,
71+
),
72+
V1Beta1CRDItems: items,
73+
}, nil
74+
}
75+
76+
func (l *Listable) listResources() (*types.ListResult, error) {
77+
var message = fmt.Sprintf(
78+
"List resources with %s / %s: GVK %s",
79+
l.List.State.GetNamespace(),
80+
l.List.State.GetName(),
81+
l.List.State.GroupVersionKind(),
82+
)
83+
client, err := l.GetClientForAPIVersionAndKind(
84+
l.List.State.GetAPIVersion(),
85+
l.List.State.GetKind(),
86+
)
87+
if err != nil {
88+
return nil, err
89+
}
90+
items, err := client.
91+
Namespace(l.List.State.GetNamespace()).
92+
List(metav1.ListOptions{}) // TODO add label selector
93+
if err != nil {
94+
return nil, err
95+
}
96+
return &types.ListResult{
97+
Phase: types.ListStatusPassed,
98+
Message: message,
99+
Items: items,
100+
}, nil
101+
}
102+
103+
// Run executes applying the desired state against the
104+
// cluster
105+
func (l *Listable) Run() (*types.ListResult, error) {
106+
if l.List.State.GetKind() == "CustomResourceDefinition" {
107+
// list CRDs
108+
return l.listCRDs()
109+
}
110+
return l.listResources()
111+
}

types/recipe/list.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+
// List represents the desired state that needs to
27+
// be listed from the cluster
28+
type List struct {
29+
// Desired state that needs to be listed from the
30+
// Kubernetes cluster
31+
State *unstructured.Unstructured `json:"state"`
32+
}
33+
34+
// String implements the Stringer interface
35+
func (l List) String() string {
36+
raw, err := json.MarshalIndent(
37+
l,
38+
" ",
39+
".",
40+
)
41+
if err != nil {
42+
panic(err)
43+
}
44+
return string(raw)
45+
}
46+
47+
// ListStatusPhase is a typed definition to determine the
48+
// result of executing a list invocation
49+
type ListStatusPhase string
50+
51+
const (
52+
// ListStatusPassed defines a successful list
53+
ListStatusPassed ListStatusPhase = "Passed"
54+
55+
// ListStatusWarning defines a list that resulted in warnings
56+
ListStatusWarning ListStatusPhase = "Warning"
57+
58+
// ListStatusFailed defines a failed list
59+
ListStatusFailed ListStatusPhase = "Failed"
60+
)
61+
62+
// ToTaskStatusPhase transforms ListStatusPhase to TaskStatusPhase
63+
func (phase ListStatusPhase) ToTaskStatusPhase() TaskStatusPhase {
64+
switch phase {
65+
case ListStatusPassed:
66+
return TaskStatusPassed
67+
case ListStatusFailed:
68+
return TaskStatusFailed
69+
case ListStatusWarning:
70+
return TaskStatusWarning
71+
default:
72+
return ""
73+
}
74+
}
75+
76+
// ListResult holds the result of the list operation
77+
type ListResult struct {
78+
Phase ListStatusPhase `json:"phase"`
79+
Message string `json:"message,omitempty"`
80+
Verbose string `json:"verbose,omitempty"`
81+
Warning string `json:"warning,omitempty"`
82+
V1Beta1CRDItems *v1beta1.CustomResourceDefinitionList `json:"v1b1CRDs,omitempty"`
83+
Items *unstructured.UnstructuredList `json:"items,omitempty"`
84+
}

0 commit comments

Comments
 (0)