|
| 1 | +package kubernetes |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + |
| 7 | + "github.com/1Password/shell-plugins/sdk" |
| 8 | + "github.com/1Password/shell-plugins/sdk/importer" |
| 9 | + "github.com/1Password/shell-plugins/sdk/provision" |
| 10 | + "github.com/1Password/shell-plugins/sdk/schema" |
| 11 | + "github.com/1Password/shell-plugins/sdk/schema/fieldname" |
| 12 | +) |
| 13 | + |
| 14 | +func Kubeconfig() schema.CredentialType { |
| 15 | + return schema.CredentialType{ |
| 16 | + Name: sdk.CredentialName("Kubeconfig"), |
| 17 | + DocsURL: sdk.URL("https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/"), |
| 18 | + Fields: []schema.CredentialField{ |
| 19 | + { |
| 20 | + Name: fieldname.Credential, |
| 21 | + MarkdownDescription: "Base64-encoded kubeconfig YAML file contents.", |
| 22 | + Secret: true, |
| 23 | + }, |
| 24 | + }, |
| 25 | + DefaultProvisioner: provision.TempFile( |
| 26 | + kubeconfigFileContents, |
| 27 | + provision.Filename("config"), |
| 28 | + provision.SetPathAsEnvVar("KUBECONFIG"), |
| 29 | + ), |
| 30 | + Importer: importer.TryAll( |
| 31 | + TryKubeconfigFile(), |
| 32 | + ), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func kubeconfigFileContents(in sdk.ProvisionInput) ([]byte, error) { |
| 37 | + encoded := in.ItemFields[fieldname.Credential] |
| 38 | + decoded, err := base64.StdEncoding.DecodeString(encoded) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + return decoded, nil |
| 43 | +} |
| 44 | + |
| 45 | +func TryKubeconfigFile() sdk.Importer { |
| 46 | + return importer.TryFile("~/.kube/config", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { |
| 47 | + // Import existing kubeconfig as base64-encoded |
| 48 | + encoded := base64.StdEncoding.EncodeToString(contents) |
| 49 | + out.AddCandidate(sdk.ImportCandidate{ |
| 50 | + Fields: map[sdk.FieldName]string{ |
| 51 | + fieldname.Credential: encoded, |
| 52 | + }, |
| 53 | + NameHint: importer.SanitizeNameHint("default"), |
| 54 | + }) |
| 55 | + }) |
| 56 | +} |
0 commit comments