|
| 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 | +} |
0 commit comments