|
| 1 | +/* |
| 2 | +Copyright 2020 Cortex Labs, Inc. |
| 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 | + http://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 k8s |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + "github.com/cortexlabs/cortex/pkg/lib/errors" |
| 23 | + kcore "k8s.io/api/core/v1" |
| 24 | + kerrors "k8s.io/apimachinery/pkg/api/errors" |
| 25 | + kmeta "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + klabels "k8s.io/apimachinery/pkg/labels" |
| 27 | +) |
| 28 | + |
| 29 | +var _secretTypeMeta = kmeta.TypeMeta{ |
| 30 | + APIVersion: "v1", |
| 31 | + Kind: "Secret", |
| 32 | +} |
| 33 | + |
| 34 | +type SecretSpec struct { |
| 35 | + Name string |
| 36 | + Data map[string][]byte |
| 37 | + Labels map[string]string |
| 38 | + Annotations map[string]string |
| 39 | +} |
| 40 | + |
| 41 | +func Secret(spec *SecretSpec) *kcore.Secret { |
| 42 | + secret := &kcore.Secret{ |
| 43 | + TypeMeta: _secretTypeMeta, |
| 44 | + ObjectMeta: kmeta.ObjectMeta{ |
| 45 | + Name: spec.Name, |
| 46 | + Labels: spec.Labels, |
| 47 | + Annotations: spec.Annotations, |
| 48 | + }, |
| 49 | + Data: spec.Data, |
| 50 | + } |
| 51 | + return secret |
| 52 | +} |
| 53 | + |
| 54 | +func (c *Client) CreateSecret(secret *kcore.Secret) (*kcore.Secret, error) { |
| 55 | + secret.TypeMeta = _secretTypeMeta |
| 56 | + secret, err := c.secretClient.Create(context.Background(), secret, kmeta.CreateOptions{}) |
| 57 | + if err != nil { |
| 58 | + return nil, errors.WithStack(err) |
| 59 | + } |
| 60 | + return secret, nil |
| 61 | +} |
| 62 | + |
| 63 | +func (c *Client) UpdateSecret(secret *kcore.Secret) (*kcore.Secret, error) { |
| 64 | + secret.TypeMeta = _secretTypeMeta |
| 65 | + secret, err := c.secretClient.Update(context.Background(), secret, kmeta.UpdateOptions{}) |
| 66 | + if err != nil { |
| 67 | + return nil, errors.WithStack(err) |
| 68 | + } |
| 69 | + return secret, nil |
| 70 | +} |
| 71 | + |
| 72 | +func (c *Client) ApplySecret(secret *kcore.Secret) (*kcore.Secret, error) { |
| 73 | + existing, err := c.GetSecret(secret.Name) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + if existing == nil { |
| 78 | + return c.CreateSecret(secret) |
| 79 | + } |
| 80 | + return c.UpdateSecret(secret) |
| 81 | +} |
| 82 | + |
| 83 | +func (c *Client) GetSecret(name string) (*kcore.Secret, error) { |
| 84 | + secret, err := c.secretClient.Get(context.Background(), name, kmeta.GetOptions{}) |
| 85 | + if kerrors.IsNotFound(err) { |
| 86 | + return nil, nil |
| 87 | + } |
| 88 | + if err != nil { |
| 89 | + return nil, errors.WithStack(err) |
| 90 | + } |
| 91 | + secret.TypeMeta = _secretTypeMeta |
| 92 | + return secret, nil |
| 93 | +} |
| 94 | + |
| 95 | +func (c *Client) GetSecretData(name string) (map[string][]byte, error) { |
| 96 | + secret, err := c.GetSecret(name) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + if secret == nil { |
| 101 | + return nil, nil |
| 102 | + } |
| 103 | + return secret.Data, nil |
| 104 | +} |
| 105 | + |
| 106 | +func (c *Client) DeleteSecret(name string) (bool, error) { |
| 107 | + err := c.secretClient.Delete(context.Background(), name, _deleteOpts) |
| 108 | + if kerrors.IsNotFound(err) { |
| 109 | + return false, nil |
| 110 | + } |
| 111 | + if err != nil { |
| 112 | + return false, errors.WithStack(err) |
| 113 | + } |
| 114 | + return true, nil |
| 115 | +} |
| 116 | + |
| 117 | +func (c *Client) ListSecrets(opts *kmeta.ListOptions) ([]kcore.Secret, error) { |
| 118 | + if opts == nil { |
| 119 | + opts = &kmeta.ListOptions{} |
| 120 | + } |
| 121 | + secretList, err := c.secretClient.List(context.Background(), *opts) |
| 122 | + if err != nil { |
| 123 | + return nil, errors.WithStack(err) |
| 124 | + } |
| 125 | + for i := range secretList.Items { |
| 126 | + secretList.Items[i].TypeMeta = _secretTypeMeta |
| 127 | + } |
| 128 | + return secretList.Items, nil |
| 129 | +} |
| 130 | + |
| 131 | +func (c *Client) ListSecretsByLabels(labels map[string]string) ([]kcore.Secret, error) { |
| 132 | + opts := &kmeta.ListOptions{ |
| 133 | + LabelSelector: klabels.SelectorFromSet(labels).String(), |
| 134 | + } |
| 135 | + return c.ListSecrets(opts) |
| 136 | +} |
| 137 | + |
| 138 | +func (c *Client) ListSecretsByLabel(labelKey string, labelValue string) ([]kcore.Secret, error) { |
| 139 | + return c.ListSecretsByLabels(map[string]string{labelKey: labelValue}) |
| 140 | +} |
| 141 | + |
| 142 | +func (c *Client) ListSecretsWithLabelKeys(labelKeys ...string) ([]kcore.Secret, error) { |
| 143 | + opts := &kmeta.ListOptions{ |
| 144 | + LabelSelector: LabelExistsSelector(labelKeys...), |
| 145 | + } |
| 146 | + return c.ListSecrets(opts) |
| 147 | +} |
| 148 | + |
| 149 | +func SecretMap(secrets []kcore.Secret) map[string]kcore.Secret { |
| 150 | + secretMap := map[string]kcore.Secret{} |
| 151 | + for _, secret := range secrets { |
| 152 | + secretMap[secret.Name] = secret |
| 153 | + } |
| 154 | + return secretMap |
| 155 | +} |
0 commit comments