-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add secret HTTP apis: "SecretCreate", "SecretGet", "SecretList", "SecretUpdate" and "SecretDelete". Ref: 5411 Signed-off-by: James Lu <james.lu@suse.com>
- Loading branch information
1 parent
fe815cc
commit b7b1232
Showing
5 changed files
with
325 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/pkg/errors" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"github.com/rancher/go-rancher/api" | ||
"github.com/rancher/go-rancher/client" | ||
|
||
"github.com/longhorn/longhorn-manager/util" | ||
) | ||
|
||
func (s *Server) SecretList(rw http.ResponseWriter, req *http.Request) (err error) { | ||
apiContext := api.GetApiContext(req) | ||
|
||
secrets, err := s.secretList(apiContext) | ||
if err != nil { | ||
return err | ||
} | ||
apiContext.Write(secrets) | ||
return nil | ||
} | ||
|
||
func (s *Server) secretList(apiContext *api.ApiContext) (*client.GenericCollection, error) { | ||
list, err := s.m.ListSecretsSorted() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to list secrets") | ||
} | ||
return toSecretCollection(list, apiContext), nil | ||
} | ||
|
||
func (s *Server) SecretGet(rw http.ResponseWriter, req *http.Request) error { | ||
apiContext := api.GetApiContext(req) | ||
|
||
secretName := mux.Vars(req)["name"] | ||
|
||
secret, err := s.m.GetSecret(secretName) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to get secret '%s'", secretName) | ||
} | ||
apiContext.Write(toSecretResource(secret, apiContext)) | ||
return nil | ||
} | ||
|
||
func (s *Server) SecretCreate(rw http.ResponseWriter, req *http.Request) error { | ||
var input SecretInput | ||
apiContext := api.GetApiContext(req) | ||
|
||
if err := apiContext.Read(&input); err != nil { | ||
return err | ||
} | ||
|
||
secretData := make(map[string][]byte, len(input.Data)) | ||
for key, data := range input.Data { | ||
decodeData, err := base64.StdEncoding.DecodeString(data) | ||
if err != nil { | ||
return err | ||
} | ||
secretData[key] = decodeData | ||
} | ||
|
||
obj, err := s.m.CreateSecret(&corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: input.Name, | ||
}, | ||
Data: secretData, | ||
Type: corev1.SecretType(input.Type), | ||
}) | ||
if err != nil { | ||
return errors.Wrapf(err, "failed to create secret %v", input.Name) | ||
} | ||
apiContext.Write(toSecretResource(obj, apiContext)) | ||
return nil | ||
} | ||
|
||
func (s *Server) SecretUpdate(rw http.ResponseWriter, req *http.Request) error { | ||
var input SecretInput | ||
|
||
apiContext := api.GetApiContext(req) | ||
if err := apiContext.Read(&input); err != nil { | ||
return err | ||
} | ||
|
||
secretName := mux.Vars(req)["name"] | ||
secretData := make(map[string][]byte, len(input.Data)) | ||
for key, data := range input.Data { | ||
decodeData, err := base64.StdEncoding.DecodeString(data) | ||
if err != nil { | ||
return err | ||
} | ||
secretData[key] = decodeData | ||
} | ||
obj, err := util.RetryOnConflictCause(func() (interface{}, error) { | ||
return s.m.UpdateSecret(&corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: secretName, | ||
}, | ||
Data: secretData, | ||
Type: corev1.SecretType(input.Type), | ||
}) | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
secret, ok := obj.(*corev1.Secret) | ||
if !ok { | ||
return fmt.Errorf("failed to convert %v to secret object", secretName) | ||
} | ||
|
||
apiContext.Write(toSecretResource(secret, apiContext)) | ||
return nil | ||
} | ||
|
||
func (s *Server) SecretDelete(rw http.ResponseWriter, req *http.Request) error { | ||
secretName := mux.Vars(req)["name"] | ||
if err := s.m.DeleteSecret(secretName); err != nil { | ||
return errors.Wrapf(err, "failed to delete secret %v", secretName) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package manager | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
|
||
"github.com/longhorn/longhorn-manager/util" | ||
) | ||
|
||
func (m *VolumeManager) GetSecret(name string) (*corev1.Secret, error) { | ||
namespace, err := m.ds.GetLonghornNamespace() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return m.ds.GetSecretRO(namespace.Name, name) | ||
} | ||
|
||
func (m *VolumeManager) ListSecretsSorted() ([]*corev1.Secret, error) { | ||
namespace, err := m.ds.GetLonghornNamespace() | ||
if err != nil { | ||
return nil, err | ||
} | ||
secretList, err := m.ds.ListSecretsRO(namespace.Name) | ||
if err != nil { | ||
return []*corev1.Secret{}, err | ||
} | ||
|
||
itemMap := map[string]*corev1.Secret{} | ||
for _, itemRO := range secretList { | ||
itemMap[itemRO.Name] = itemRO | ||
} | ||
|
||
secrets := make([]*corev1.Secret, len(secretList)) | ||
sortedSecrets, err := util.SortKeys(itemMap) | ||
if err != nil { | ||
return []*corev1.Secret{}, err | ||
} | ||
for i, name := range sortedSecrets { | ||
secrets[i] = itemMap[name] | ||
} | ||
return secrets, nil | ||
} | ||
|
||
func (m *VolumeManager) CreateSecret(secret *corev1.Secret) (*corev1.Secret, error) { | ||
namespace, err := m.ds.GetLonghornNamespace() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
s, err := m.ds.CreateSecret(namespace.Name, secret) | ||
if err != nil { | ||
return nil, err | ||
} | ||
logrus.Infof("Created secret %v", secret.Name) | ||
return s, nil | ||
} | ||
|
||
func (m *VolumeManager) UpdateSecret(secret *corev1.Secret) (*corev1.Secret, error) { | ||
var err error | ||
defer func() { | ||
err = errors.Wrapf(err, "unable to update %v secret", secret.Name) | ||
}() | ||
namespace, err := m.ds.GetLonghornNamespace() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
existingSecret, err := m.ds.GetSecret(namespace.Name, secret.Name) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to get secret %v", secret.Name) | ||
} | ||
existingSecretDataSorted, err := util.SortKeys(existingSecret.Data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
secretDataSorted, err := util.SortKeys(secret.Data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if existingSecret.Type == secret.Type && | ||
reflect.DeepEqual(existingSecretDataSorted, secretDataSorted) { | ||
return secret, nil | ||
} | ||
|
||
return m.ds.UpdateSecret(namespace.Name, secret) | ||
} | ||
|
||
func (m *VolumeManager) DeleteSecret(name string) error { | ||
namespace, err := m.ds.GetLonghornNamespace() | ||
if err != nil { | ||
return err | ||
} | ||
if err := m.ds.DeleteSecret(namespace.Name, name); err != nil { | ||
return err | ||
} | ||
logrus.Infof("Deleted secret %v", name) | ||
return nil | ||
} |