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