secret.go 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

M
Marcin Maciaszczyk 已提交
15
package secret
16 17

import (
18
	"k8s.io/kubernetes/pkg/api"
19 20 21
	client "k8s.io/kubernetes/pkg/client/unversioned"
	"k8s.io/kubernetes/pkg/fields"
	"k8s.io/kubernetes/pkg/labels"
22
	"github.com/kubernetes/dashboard/src/app/backend/resource/common"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
)

// SecretSpec - common interface for the specification of different secrets.
type SecretSpec interface {
	GetName() string
	GetType() api.SecretType
	GetNamespace() string
	GetData() map[string][]byte
}

// ImagePullSecretSpec - specification of an image pull secret
// implements SecretSpec
type ImagePullSecretSpec struct {
	Name      string `json:"name"`
	Namespace string `json:"namespace"`
	// The value of the .dockercfg property. It must be Base64 encoded.
	Data []byte `json:"data"`
}

// GetName - return the name of the ImagePullSecret
func (spec *ImagePullSecretSpec) GetName() string {
	return spec.Name
}

// GetType - return the type of the ImagePullSecret,
// which is always api.SecretTypeDockercfg
func (spec *ImagePullSecretSpec) GetType() api.SecretType {
	return api.SecretTypeDockercfg
}

// GetNamespace - return the namespace of the ImagePullSecret
func (spec *ImagePullSecretSpec) GetNamespace() string {
	return spec.Namespace
}

// GetData - return the data the secret carries, it is a single
// key-value pair
func (spec *ImagePullSecretSpec) GetData() map[string][]byte {
	return map[string][]byte{api.DockerConfigKey: spec.Data}
}

// Secret - a single secret returned to the frontend.
type Secret struct {
66 67
	common.ObjectMeta `json:"objectMeta"`
	common.TypeMeta   `json:"typeMeta"`
68 69 70 71
}

// SecretsList - response structure for a queried secrets list.
type SecretsList struct {
72
	Secrets []Secret `json:"secrets"`
73 74 75 76
}

// GetSecrets - return all secrets in the given namespace.
func GetSecrets(client *client.Client, namespace string) (*SecretsList,
77
error) {
78
	secretsList := &SecretsList{}
B
bryk 已提交
79 80 81
	secrets, err := client.Secrets(namespace).List(api.ListOptions{
		LabelSelector: labels.Everything(),
		FieldSelector: fields.Everything(),
82 83 84 85 86
	})
	if err != nil {
		return nil, err
	}
	for _, secret := range secrets.Items {
87
		secretsList.Secrets = append(secretsList.Secrets, *NewSecret(&secret))
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	}
	return secretsList, err
}

// CreateSecret - create a single secret using the cluster API client
func CreateSecret(client *client.Client, spec SecretSpec) (*Secret, error) {
	namespace := spec.GetNamespace()
	secret := &api.Secret{
		ObjectMeta: api.ObjectMeta{
			Name:      spec.GetName(),
			Namespace: namespace,
		},
		Type: spec.GetType(),
		Data: spec.GetData(),
	}
	_, err := client.Secrets(namespace).Create(secret)
104
	return NewSecret(secret), err
105
}
106 107 108 109 110 111

// NewSecret - creates a new instance of Secret struct based on K8s Secret.
func NewSecret(secret *api.Secret) (*Secret) {
	return &Secret{common.NewObjectMeta(secret.ObjectMeta),
		common.NewTypeMeta(common.ResourceKindSecret)}
}