serviceaccount.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package versioned
  14. import (
  15. "fmt"
  16. "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. "k8s.io/kubernetes/pkg/kubectl/generate"
  19. )
  20. // ServiceAccountGeneratorV1 supports stable generation of a service account
  21. type ServiceAccountGeneratorV1 struct {
  22. // Name of service account
  23. Name string
  24. }
  25. // Ensure it supports the generator pattern that uses parameters specified during construction
  26. var _ generate.StructuredGenerator = &ServiceAccountGeneratorV1{}
  27. // StructuredGenerate outputs a service account object using the configured fields
  28. func (g *ServiceAccountGeneratorV1) StructuredGenerate() (runtime.Object, error) {
  29. if err := g.validate(); err != nil {
  30. return nil, err
  31. }
  32. serviceAccount := &v1.ServiceAccount{}
  33. serviceAccount.Name = g.Name
  34. return serviceAccount, nil
  35. }
  36. // validate validates required fields are set to support structured generation
  37. func (g *ServiceAccountGeneratorV1) validate() error {
  38. if len(g.Name) == 0 {
  39. return fmt.Errorf("name must be specified")
  40. }
  41. return nil
  42. }