diff --git a/pkg/oc/cli/secrets/basicauth.go b/pkg/oc/cli/secrets/basicauth.go index 9f994371ae2e..6d3d346bf3ae 100644 --- a/pkg/oc/cli/secrets/basicauth.go +++ b/pkg/oc/cli/secrets/basicauth.go @@ -3,19 +3,20 @@ package secrets import ( "errors" "fmt" - "io" "io/ioutil" "github.com/spf13/cobra" - api "k8s.io/kubernetes/pkg/apis/core" - kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion" + coreapiv1 "k8s.io/api/core/v1" + coreclientv1 "k8s.io/client-go/kubernetes/typed/core/v1" "k8s.io/kubernetes/pkg/kubectl/cmd/templates" kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/kubectl/genericclioptions" + "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers" kterm "k8s.io/kubernetes/pkg/kubectl/util/term" "github.com/openshift/origin/pkg/cmd/util/term" + "github.com/openshift/origin/pkg/oc/util/ocscheme" ) // CreateBasicAuthSecretRecommendedCommandName represents name of subcommand for `oc secrets` command @@ -44,6 +45,10 @@ var ( // CreateBasicAuthSecretOptions holds the credential needed to authenticate against SCM servers. type CreateBasicAuthSecretOptions struct { + PrintFlags *genericclioptions.PrintFlags + + Printer printers.ResourcePrinter + SecretName string Username string Password string @@ -52,18 +57,21 @@ type CreateBasicAuthSecretOptions struct { PromptForPassword bool - Reader io.Reader - Out io.Writer + SecretsInterface coreclientv1.SecretInterface - SecretsInterface kcoreclient.SecretInterface + genericclioptions.IOStreams +} + +func NewCreateBasicAuthSecretOptions(streams genericclioptions.IOStreams) *CreateBasicAuthSecretOptions { + return &CreateBasicAuthSecretOptions{ + PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(ocscheme.PrintingInternalScheme), + IOStreams: streams, + } } // NewCmdCreateBasicAuthSecret implements the OpenShift cli secrets new-basicauth subcommand func NewCmdCreateBasicAuthSecret(name, fullName string, f kcmdutil.Factory, streams genericclioptions.IOStreams, newSecretFullName, ocEditFullName string) *cobra.Command { - o := &CreateBasicAuthSecretOptions{ - Out: streams.Out, - Reader: streams.In, - } + o := NewCreateBasicAuthSecretOptions(streams) cmd := &cobra.Command{ Use: fmt.Sprintf("%s SECRET --username=USERNAME --password=PASSWORD [--ca-cert=FILENAME] [--gitconfig=FILENAME]", name), @@ -81,15 +89,7 @@ func NewCmdCreateBasicAuthSecret(name, fullName string, f kcmdutil.Factory, stre kcmdutil.CheckErr(kcmdutil.UsageErrorf(c, err.Error())) } - if len(kcmdutil.GetFlagString(c, "output")) != 0 { - secret, err := o.NewBasicAuthSecret() - kcmdutil.CheckErr(err) - - kcmdutil.CheckErr(kcmdutil.PrintObject(c, secret, streams.Out)) - return - } - - if err := o.CreateBasicAuthSecret(); err != nil { + if err := o.Run(); err != nil { kcmdutil.CheckErr(err) } }, @@ -103,13 +103,12 @@ func NewCmdCreateBasicAuthSecret(name, fullName string, f kcmdutil.Factory, stre cmd.MarkFlagFilename("gitconfig") cmd.Flags().BoolVarP(&o.PromptForPassword, "prompt", "", false, "If true, prompt for password or token") - kcmdutil.AddPrinterFlags(cmd) - + o.PrintFlags.AddFlags(cmd) return cmd } // CreateBasicAuthSecret saves created Secret structure and prints the secret name to the output on success. -func (o *CreateBasicAuthSecretOptions) CreateBasicAuthSecret() error { +func (o *CreateBasicAuthSecretOptions) Run() error { secret, err := o.NewBasicAuthSecret() if err != nil { return err @@ -119,16 +118,15 @@ func (o *CreateBasicAuthSecretOptions) CreateBasicAuthSecret() error { return err } - fmt.Fprintf(o.GetOut(), "secret/%s\n", secret.Name) - return nil + return o.Printer.PrintObj(secret, o.Out) } // NewBasicAuthSecret builds up the Secret structure containing secret name, type and data structure // containing desired credentials. -func (o *CreateBasicAuthSecretOptions) NewBasicAuthSecret() (*api.Secret, error) { - secret := &api.Secret{} +func (o *CreateBasicAuthSecretOptions) NewBasicAuthSecret() (*coreapiv1.Secret, error) { + secret := &coreapiv1.Secret{} secret.Name = o.SecretName - secret.Type = api.SecretTypeBasicAuth + secret.Type = coreapiv1.SecretTypeBasicAuth secret.Data = map[string][]byte{} if len(o.Username) != 0 { @@ -170,26 +168,34 @@ func (o *CreateBasicAuthSecretOptions) Complete(f kcmdutil.Factory, args []strin if len(o.Password) != 0 { return errors.New("must provide either --prompt or --password flag") } - if !kterm.IsTerminal(o.Reader) { + if !kterm.IsTerminal(o.In) { return errors.New("provided reader is not a terminal") } - o.Password = term.PromptForPasswordString(o.Reader, o.Out, "Password: ") + o.Password = term.PromptForPasswordString(o.In, o.Out, "Password: ") if len(o.Password) == 0 { return errors.New("password must be provided") } } - if f != nil { - client, err := f.ClientSet() - if err != nil { - return err - } - namespace, _, err := f.ToRawKubeConfigLoader().Namespace() - if err != nil { - return err - } - o.SecretsInterface = client.Core().Secrets(namespace) + config, err := f.ToRESTConfig() + if err != nil { + return err + } + + clientset, err := coreclientv1.NewForConfig(config) + if err != nil { + return err + } + namespace, _, err := f.ToRawKubeConfigLoader().Namespace() + if err != nil { + return err + } + o.SecretsInterface = clientset.Secrets(namespace) + + o.Printer, err = o.PrintFlags.ToPrinter() + if err != nil { + return err } return nil @@ -207,13 +213,3 @@ func (o CreateBasicAuthSecretOptions) Validate() error { return nil } - -// GetOut check if the CreateBasicAuthSecretOptions Out Writer is set. Returns it if the Writer -// is present, if not returns Writer on which all Write calls succeed without doing anything. -func (o CreateBasicAuthSecretOptions) GetOut() io.Writer { - if o.Out == nil { - return ioutil.Discard - } - - return o.Out -} diff --git a/pkg/oc/cli/secrets/dockercfg.go b/pkg/oc/cli/secrets/dockercfg.go index dc5fb1912b31..c8ce55b70167 100644 --- a/pkg/oc/cli/secrets/dockercfg.go +++ b/pkg/oc/cli/secrets/dockercfg.go @@ -4,18 +4,18 @@ import ( "encoding/json" "errors" "fmt" - "io" - "io/ioutil" "strings" - api "k8s.io/kubernetes/pkg/apis/core" - kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion" + "github.com/spf13/cobra" + + "github.com/openshift/origin/pkg/oc/util/ocscheme" + coreapiv1 "k8s.io/api/core/v1" + coreclientv1 "k8s.io/client-go/kubernetes/typed/core/v1" "k8s.io/kubernetes/pkg/credentialprovider" "k8s.io/kubernetes/pkg/kubectl/cmd/templates" kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/kubectl/genericclioptions" - - "github.com/spf13/cobra" + "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers" ) const CreateDockerConfigSecretRecommendedName = "new-dockercfg" @@ -50,20 +50,31 @@ var ( ) type CreateDockerConfigOptions struct { + PrintFlags *genericclioptions.PrintFlags + + Printer printers.ResourcePrinter + SecretName string RegistryLocation string Username string Password string EmailAddress string - SecretsInterface kcoreclient.SecretInterface + SecretsInterface coreclientv1.SecretInterface + + genericclioptions.IOStreams +} - Out io.Writer +func NewCreateDockerConfigOptions(streams genericclioptions.IOStreams) *CreateDockerConfigOptions { + return &CreateDockerConfigOptions{ + PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(ocscheme.PrintingInternalScheme), + IOStreams: streams, + } } // NewCmdCreateDockerConfigSecret creates a command object for making a dockercfg secret func NewCmdCreateDockerConfigSecret(name, fullName string, f kcmdutil.Factory, streams genericclioptions.IOStreams, newSecretFullName, ocEditFullName string) *cobra.Command { - o := &CreateDockerConfigOptions{Out: streams.Out} + o := NewCreateDockerConfigOptions(streams) cmd := &cobra.Command{ Use: fmt.Sprintf("%s SECRET --docker-server=DOCKER_REGISTRY_SERVER --docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL", name), @@ -81,15 +92,7 @@ func NewCmdCreateDockerConfigSecret(name, fullName string, f kcmdutil.Factory, s kcmdutil.CheckErr(kcmdutil.UsageErrorf(c, err.Error())) } - if len(kcmdutil.GetFlagString(c, "output")) != 0 { - secret, err := o.NewDockerSecret() - kcmdutil.CheckErr(err) - - kcmdutil.CheckErr(kcmdutil.PrintObject(c, secret, streams.Out)) - return - } - - if err := o.CreateDockerSecret(); err != nil { + if err := o.Run(); err != nil { kcmdutil.CheckErr(err) } @@ -100,12 +103,12 @@ func NewCmdCreateDockerConfigSecret(name, fullName string, f kcmdutil.Factory, s cmd.Flags().StringVar(&o.Password, "docker-password", "", "Password for Docker registry authentication") cmd.Flags().StringVar(&o.EmailAddress, "docker-email", "", "Email for Docker registry") cmd.Flags().StringVar(&o.RegistryLocation, "docker-server", "https://index.docker.io/v1/", "Server location for Docker registry") - kcmdutil.AddPrinterFlags(cmd) + o.PrintFlags.AddFlags(cmd) return cmd } -func (o CreateDockerConfigOptions) CreateDockerSecret() error { +func (o CreateDockerConfigOptions) Run() error { secret, err := o.NewDockerSecret() if err != nil { return err @@ -115,12 +118,10 @@ func (o CreateDockerConfigOptions) CreateDockerSecret() error { return err } - fmt.Fprintf(o.GetOut(), "secret/%s\n", secret.Name) - - return nil + return o.Printer.PrintObj(secret, o.Out) } -func (o CreateDockerConfigOptions) NewDockerSecret() (*api.Secret, error) { +func (o CreateDockerConfigOptions) NewDockerSecret() (*coreapiv1.Secret, error) { dockercfgAuth := credentialprovider.DockerConfigEntry{ Username: o.Username, Password: o.Password, @@ -136,11 +137,11 @@ func (o CreateDockerConfigOptions) NewDockerSecret() (*api.Secret, error) { return nil, err } - secret := &api.Secret{} + secret := &coreapiv1.Secret{} secret.Name = o.SecretName - secret.Type = api.SecretTypeDockerConfigJson + secret.Type = coreapiv1.SecretTypeDockerConfigJson secret.Data = map[string][]byte{} - secret.Data[api.DockerConfigJsonKey] = dockercfgContent + secret.Data[coreapiv1.DockerConfigJsonKey] = dockercfgContent return secret, nil } @@ -151,7 +152,12 @@ func (o *CreateDockerConfigOptions) Complete(f kcmdutil.Factory, args []string) } o.SecretName = args[0] - client, err := f.ClientSet() + config, err := f.ToRESTConfig() + if err != nil { + return err + } + + client, err := coreclientv1.NewForConfig(config) if err != nil { return err } @@ -160,7 +166,12 @@ func (o *CreateDockerConfigOptions) Complete(f kcmdutil.Factory, args []string) return err } - o.SecretsInterface = client.Core().Secrets(namespace) + o.SecretsInterface = client.Secrets(namespace) + + o.Printer, err = o.PrintFlags.ToPrinter() + if err != nil { + return err + } return nil } @@ -191,11 +202,3 @@ func (o CreateDockerConfigOptions) Validate() error { return nil } - -func (o CreateDockerConfigOptions) GetOut() io.Writer { - if o.Out == nil { - return ioutil.Discard - } - - return o.Out -} diff --git a/pkg/oc/cli/secrets/known_secret_types.go b/pkg/oc/cli/secrets/known_secret_types.go index d2eea3bbb5b4..f9746237fc76 100644 --- a/pkg/oc/cli/secrets/known_secret_types.go +++ b/pkg/oc/cli/secrets/known_secret_types.go @@ -3,12 +3,12 @@ package secrets import ( "reflect" + coreapiv1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/util/sets" - kapi "k8s.io/kubernetes/pkg/apis/core" ) type KnownSecretType struct { - Type kapi.SecretType + Type coreapiv1.SecretType RequiredContents sets.String } @@ -22,7 +22,7 @@ func (ks KnownSecretType) Matches(secretContent map[string][]byte) bool { var ( KnownSecretTypes = []KnownSecretType{ - {kapi.SecretTypeDockercfg, sets.NewString(kapi.DockerConfigKey)}, - {kapi.SecretTypeDockerConfigJson, sets.NewString(kapi.DockerConfigJsonKey)}, + {coreapiv1.SecretTypeDockercfg, sets.NewString(coreapiv1.DockerConfigKey)}, + {coreapiv1.SecretTypeDockerConfigJson, sets.NewString(coreapiv1.DockerConfigJsonKey)}, } ) diff --git a/pkg/oc/cli/secrets/link_secret_to_obj.go b/pkg/oc/cli/secrets/link_secret_to_obj.go index 2cf4f1ac1537..dc3753d0e6b0 100644 --- a/pkg/oc/cli/secrets/link_secret_to_obj.go +++ b/pkg/oc/cli/secrets/link_secret_to_obj.go @@ -6,7 +6,7 @@ import ( "os" "strings" - kapi "k8s.io/kubernetes/pkg/apis/core" + coreapiv1 "k8s.io/api/core/v1" "k8s.io/kubernetes/pkg/kubectl/cmd/templates" kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/kubectl/genericclioptions" @@ -43,9 +43,15 @@ type LinkSecretOptions struct { typeFlags []string } +func NewLinkSecretOptions(streams genericclioptions.IOStreams) *LinkSecretOptions { + return &LinkSecretOptions{ + SecretOptions: SecretOptions{}, + } +} + // NewCmdLinkSecret creates a command object for linking a secret reference to a service account func NewCmdLinkSecret(name, fullName string, f kcmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command { - o := &LinkSecretOptions{SecretOptions{Out: streams.Out}, false, false, nil} + o := NewLinkSecretOptions(streams) cmd := &cobra.Command{ Use: fmt.Sprintf("%s serviceaccounts-name secret-name [another-secret-name]...", name), @@ -131,7 +137,7 @@ func (o LinkSecretOptions) LinkSecrets() error { // TODO: when Secrets in kapi.ServiceAccount get changed to MountSecrets and represented by LocalObjectReferences, this can be // refactored to reuse the addition code better // linkSecretsToServiceAccount links secrets to the service account, either as pull secrets, mount secrets, or both. -func (o LinkSecretOptions) linkSecretsToServiceAccount(serviceaccount *kapi.ServiceAccount) error { +func (o LinkSecretOptions) linkSecretsToServiceAccount(serviceaccount *coreapiv1.ServiceAccount) error { updated := false newSecrets, hasNotFound, err := o.GetSecrets(false) if err != nil { @@ -143,7 +149,7 @@ func (o LinkSecretOptions) linkSecretsToServiceAccount(serviceaccount *kapi.Serv currentSecrets := o.GetMountSecretNames(serviceaccount) secretsToLink := newSecretNames.Difference(currentSecrets) for _, secretName := range secretsToLink.List() { - serviceaccount.Secrets = append(serviceaccount.Secrets, kapi.ObjectReference{Name: secretName}) + serviceaccount.Secrets = append(serviceaccount.Secrets, coreapiv1.ObjectReference{Name: secretName}) updated = true } } @@ -151,7 +157,7 @@ func (o LinkSecretOptions) linkSecretsToServiceAccount(serviceaccount *kapi.Serv currentSecrets := o.GetPullSecretNames(serviceaccount) secretsToLink := newSecretNames.Difference(currentSecrets) for _, secretName := range secretsToLink.List() { - serviceaccount.ImagePullSecrets = append(serviceaccount.ImagePullSecrets, kapi.LocalObjectReference{Name: secretName}) + serviceaccount.ImagePullSecrets = append(serviceaccount.ImagePullSecrets, coreapiv1.LocalObjectReference{Name: secretName}) updated = true } } diff --git a/pkg/oc/cli/secrets/new.go b/pkg/oc/cli/secrets/new.go index 584720c37705..7511933a5089 100644 --- a/pkg/oc/cli/secrets/new.go +++ b/pkg/oc/cli/secrets/new.go @@ -9,15 +9,17 @@ import ( "path" "strings" + "github.com/spf13/cobra" + + "github.com/openshift/origin/pkg/oc/util/ocscheme" + coreapiv1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" kvalidation "k8s.io/apimachinery/pkg/util/validation" - kapi "k8s.io/kubernetes/pkg/apis/core" - kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion" + coreclientv1 "k8s.io/client-go/kubernetes/typed/core/v1" "k8s.io/kubernetes/pkg/kubectl/cmd/templates" kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/kubectl/genericclioptions" - - "github.com/spf13/cobra" + "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers" ) const NewSecretRecommendedCommandName = "new" @@ -48,6 +50,10 @@ var ( ) type CreateSecretOptions struct { + PrintFlags *genericclioptions.PrintFlags + + Printer printers.ResourcePrinter + // Name of the resulting secret Name string @@ -58,7 +64,7 @@ type CreateSecretOptions struct { // Directory sources are listed and any direct file children included (but subfolders are not traversed) Sources []string - SecretsInterface kcoreclient.SecretInterface + SecretsInterface coreclientv1.SecretInterface // Writer to write warnings to Stderr io.Writer @@ -69,11 +75,19 @@ type CreateSecretOptions struct { Quiet bool AllowUnknownTypes bool + + genericclioptions.IOStreams +} + +func NewCreateSecretOptions(streams genericclioptions.IOStreams) *CreateSecretOptions { + return &CreateSecretOptions{ + PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(ocscheme.PrintingInternalScheme), + IOStreams: streams, + } } func NewCmdCreateSecret(name, fullName string, f kcmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command { - options := NewCreateSecretOptions() - options.Out = streams.Out + o := NewCreateSecretOptions(streams) cmd := &cobra.Command{ Use: fmt.Sprintf("%s NAME [KEY=]SOURCE ...", name), @@ -83,42 +97,28 @@ func NewCmdCreateSecret(name, fullName string, f kcmdutil.Factory, streams gener Deprecated: "use oc create secret", Hidden: true, Run: func(c *cobra.Command, args []string) { - if err := options.Complete(args, f); err != nil { + if err := o.Complete(args, f); err != nil { kcmdutil.CheckErr(kcmdutil.UsageErrorf(c, err.Error())) } - if err := options.Validate(); err != nil { + if err := o.Validate(); err != nil { kcmdutil.CheckErr(kcmdutil.UsageErrorf(c, err.Error())) } - if len(kcmdutil.GetFlagString(c, "output")) != 0 { - secret, err := options.BundleSecret() + if err := o.Run(); err != nil { kcmdutil.CheckErr(err) - - kcmdutil.CheckErr(kcmdutil.PrintObject(c, secret, streams.Out)) - return } - - _, err := options.CreateSecret() - kcmdutil.CheckErr(err) }, } - cmd.Flags().BoolVarP(&options.Quiet, "quiet", "q", options.Quiet, "If true, suppress warnings") - cmd.Flags().BoolVar(&options.AllowUnknownTypes, "confirm", options.AllowUnknownTypes, "If true, allow unknown secret types.") - cmd.Flags().StringVar(&options.SecretTypeName, "type", "", "The type of secret") - kcmdutil.AddPrinterFlags(cmd) + cmd.Flags().BoolVarP(&o.Quiet, "quiet", "q", o.Quiet, "If true, suppress warnings") + cmd.Flags().BoolVar(&o.AllowUnknownTypes, "confirm", o.AllowUnknownTypes, "If true, allow unknown secret types.") + cmd.Flags().StringVar(&o.SecretTypeName, "type", "", "The type of secret") + o.PrintFlags.AddFlags(cmd) return cmd } -func NewCreateSecretOptions() *CreateSecretOptions { - return &CreateSecretOptions{ - Stderr: os.Stderr, - Sources: []string{}, - } -} - func (o *CreateSecretOptions) Complete(args []string, f kcmdutil.Factory) error { // Fill name from args[0] if len(args) > 0 { @@ -130,20 +130,23 @@ func (o *CreateSecretOptions) Complete(args []string, f kcmdutil.Factory) error o.Sources = append(o.Sources, args[1:]...) } - if f != nil { - clientConfig, err := f.ToRESTConfig() - if err != nil { - return err - } - kubeClient, err := kcoreclient.NewForConfig(clientConfig) - if err != nil { - return err - } - namespace, _, err := f.ToRawKubeConfigLoader().Namespace() - if err != nil { - return err - } - o.SecretsInterface = kubeClient.Secrets(namespace) + clientConfig, err := f.ToRESTConfig() + if err != nil { + return err + } + kubeClient, err := coreclientv1.NewForConfig(clientConfig) + if err != nil { + return err + } + namespace, _, err := f.ToRawKubeConfigLoader().Namespace() + if err != nil { + return err + } + o.SecretsInterface = kubeClient.Secrets(namespace) + + o.Printer, err = o.PrintFlags.ToPrinter() + if err != nil { + return err } return nil @@ -159,7 +162,7 @@ func (o *CreateSecretOptions) Validate() error { if !o.AllowUnknownTypes { switch o.SecretTypeName { - case string(kapi.SecretTypeOpaque), "": + case string(coreapiv1.SecretTypeOpaque), "": // this is ok default: found := false @@ -178,21 +181,21 @@ func (o *CreateSecretOptions) Validate() error { return nil } -func (o *CreateSecretOptions) CreateSecret() (*kapi.Secret, error) { +func (o *CreateSecretOptions) Run() error { secret, err := o.BundleSecret() if err != nil { - return nil, err + return err } persistedSecret, err := o.SecretsInterface.Create(secret) - if err == nil { - fmt.Fprintf(o.Out, "secret/%s\n", persistedSecret.Name) + if err != nil { + return err } - return persistedSecret, err + return o.Printer.PrintObj(persistedSecret, o.Out) } -func (o *CreateSecretOptions) BundleSecret() (*kapi.Secret, error) { +func (o *CreateSecretOptions) BundleSecret() (*coreapiv1.Secret, error) { secretData := make(map[string][]byte) for _, source := range o.Sources { @@ -247,9 +250,9 @@ func (o *CreateSecretOptions) BundleSecret() (*kapi.Secret, error) { } // if the secret type isn't specified, attempt to auto-detect likely hit - secretType := kapi.SecretType(o.SecretTypeName) + secretType := coreapiv1.SecretType(o.SecretTypeName) if len(o.SecretTypeName) == 0 { - secretType = kapi.SecretTypeOpaque + secretType = coreapiv1.SecretTypeOpaque for _, knownSecretType := range KnownSecretTypes { if knownSecretType.Matches(secretData) { @@ -258,7 +261,7 @@ func (o *CreateSecretOptions) BundleSecret() (*kapi.Secret, error) { } } - secret := &kapi.Secret{ + secret := &coreapiv1.Secret{ ObjectMeta: metav1.ObjectMeta{Name: o.Name}, Type: secretType, Data: secretData, diff --git a/pkg/oc/cli/secrets/options.go b/pkg/oc/cli/secrets/options.go index ba77190b529d..bd1fc8633d26 100644 --- a/pkg/oc/cli/secrets/options.go +++ b/pkg/oc/cli/secrets/options.go @@ -3,16 +3,14 @@ package secrets import ( "errors" "fmt" - "io" - "io/ioutil" "os" "strings" + corev1 "k8s.io/api/core/v1" kerrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/sets" - kapi "k8s.io/kubernetes/pkg/apis/core" - kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion" + kcoreclientv1 "k8s.io/client-go/kubernetes/typed/core/v1" kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource" @@ -29,9 +27,7 @@ type SecretOptions struct { Namespace string BuilderFunc func() *resource.Builder - KubeCoreClient kcoreclient.CoreInterface - - Out io.Writer + KubeCoreClient kcoreclientv1.CoreV1Interface } // Complete Parses the command line arguments and populates SecretOptions @@ -49,7 +45,7 @@ func (o *SecretOptions) Complete(f kcmdutil.Factory, args []string) error { if err != nil { return err } - kubeClient, err := kcoreclient.NewForConfig(clientConfig) + kubeClient, err := kcoreclientv1.NewForConfig(clientConfig) if err != nil { return err } @@ -88,9 +84,9 @@ func (o SecretOptions) Validate() error { } // GetServiceAccount Retrieve the service account object specified by the command -func (o SecretOptions) GetServiceAccount() (*kapi.ServiceAccount, error) { +func (o SecretOptions) GetServiceAccount() (*corev1.ServiceAccount, error) { r := o.BuilderFunc(). - WithScheme(ocscheme.ReadingInternalScheme). + WithScheme(ocscheme.ReadingInternalScheme, ocscheme.ReadingInternalScheme.PrioritizedVersionsAllGroups()...). NamespaceParam(o.Namespace). ResourceNames("serviceaccounts", o.TargetName). SingleResourceType(). @@ -104,7 +100,7 @@ func (o SecretOptions) GetServiceAccount() (*kapi.ServiceAccount, error) { } switch t := obj.(type) { - case *kapi.ServiceAccount: + case *corev1.ServiceAccount: return t, nil default: return nil, fmt.Errorf("unhandled object: %#v", t) @@ -112,7 +108,7 @@ func (o SecretOptions) GetServiceAccount() (*kapi.ServiceAccount, error) { } // GetSecretNames Get a list of the names of the secrets in a set of them -func (o SecretOptions) GetSecretNames(secrets []*kapi.Secret) sets.String { +func (o SecretOptions) GetSecretNames(secrets []*corev1.Secret) sets.String { names := sets.String{} for _, secret := range secrets { names.Insert(parseSecretName(secret.Name)) @@ -133,7 +129,7 @@ func parseSecretName(name string) string { // GetMountSecretNames Get a list of the names of the mount secrets associated // with a service account -func (o SecretOptions) GetMountSecretNames(serviceaccount *kapi.ServiceAccount) sets.String { +func (o SecretOptions) GetMountSecretNames(serviceaccount *corev1.ServiceAccount) sets.String { names := sets.String{} for _, secret := range serviceaccount.Secrets { names.Insert(secret.Name) @@ -143,7 +139,7 @@ func (o SecretOptions) GetMountSecretNames(serviceaccount *kapi.ServiceAccount) // GetPullSecretNames Get a list of the names of the pull secrets associated // with a service account. -func (o SecretOptions) GetPullSecretNames(serviceaccount *kapi.ServiceAccount) sets.String { +func (o SecretOptions) GetPullSecretNames(serviceaccount *corev1.ServiceAccount) sets.String { names := sets.String{} for _, secret := range serviceaccount.ImagePullSecrets { names.Insert(secret.Name) @@ -151,19 +147,10 @@ func (o SecretOptions) GetPullSecretNames(serviceaccount *kapi.ServiceAccount) s return names } -// GetOut Retrieve the output writer -func (o SecretOptions) GetOut() io.Writer { - if o.Out == nil { - return ioutil.Discard - } - - return o.Out -} - // GetSecrets Return a list of secret objects in the default namespace // If allowNonExisting is set to true, we will return the non-existing secrets as well. -func (o SecretOptions) GetSecrets(allowNonExisting bool) ([]*kapi.Secret, bool, error) { - secrets := []*kapi.Secret{} +func (o SecretOptions) GetSecrets(allowNonExisting bool) ([]*corev1.Secret, bool, error) { + secrets := []*corev1.Secret{} hasNotFound := false for _, secretName := range o.SecretNames { @@ -184,7 +171,7 @@ func (o SecretOptions) GetSecrets(allowNonExisting bool) ([]*kapi.Secret, bool, fmt.Fprintf(os.Stderr, "secret %q not found\n", secretName) hasNotFound = true if allowNonExisting { - obj = &kapi.Secret{ + obj = &corev1.Secret{ ObjectMeta: metav1.ObjectMeta{ Name: secretName, }, @@ -197,7 +184,7 @@ func (o SecretOptions) GetSecrets(allowNonExisting bool) ([]*kapi.Secret, bool, } } switch t := obj.(type) { - case *kapi.Secret: + case *corev1.Secret: secrets = append(secrets, t) default: return nil, false, fmt.Errorf("unhandled object: %#v", t) diff --git a/pkg/oc/cli/secrets/sshauth.go b/pkg/oc/cli/secrets/sshauth.go index eca058d11b98..fa2b523ddc3b 100644 --- a/pkg/oc/cli/secrets/sshauth.go +++ b/pkg/oc/cli/secrets/sshauth.go @@ -3,16 +3,17 @@ package secrets import ( "errors" "fmt" - "io" "io/ioutil" - api "k8s.io/kubernetes/pkg/apis/core" - kcoreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion" + "github.com/spf13/cobra" + + "github.com/openshift/origin/pkg/oc/util/ocscheme" + coreapiv1 "k8s.io/api/core/v1" + coreclientv1 "k8s.io/client-go/kubernetes/typed/core/v1" "k8s.io/kubernetes/pkg/kubectl/cmd/templates" kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/kubectl/genericclioptions" - - "github.com/spf13/cobra" + "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers" ) // CreateSSHAuthSecretRecommendedCommandName represents name of subcommand for `oc secrets` command @@ -41,6 +42,10 @@ var ( // CreateSSHAuthSecretOptions holds the credential needed to authenticate against SCM servers. type CreateSSHAuthSecretOptions struct { + PrintFlags *genericclioptions.PrintFlags + + Printer printers.ResourcePrinter + SecretName string PrivateKeyPath string CertificatePath string @@ -48,16 +53,21 @@ type CreateSSHAuthSecretOptions struct { PromptForPassword bool - Out io.Writer + SecretsInterface coreclientv1.SecretInterface - SecretsInterface kcoreclient.SecretInterface + genericclioptions.IOStreams +} + +func NewCreateSSHAuthSecretOptions(streams genericclioptions.IOStreams) *CreateSSHAuthSecretOptions { + return &CreateSSHAuthSecretOptions{ + PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(ocscheme.PrintingInternalScheme), + IOStreams: streams, + } } // NewCmdCreateSSHAuthSecret implements the OpenShift cli secrets new-sshauth subcommand func NewCmdCreateSSHAuthSecret(name, fullName string, f kcmdutil.Factory, streams genericclioptions.IOStreams, newSecretFullName, ocEditFullName string) *cobra.Command { - o := &CreateSSHAuthSecretOptions{ - Out: streams.Out, - } + o := NewCreateSSHAuthSecretOptions(streams) cmd := &cobra.Command{ Use: fmt.Sprintf("%s SECRET --ssh-privatekey=FILENAME [--ca-cert=FILENAME] [--gitconfig=FILENAME]", name), @@ -75,15 +85,7 @@ func NewCmdCreateSSHAuthSecret(name, fullName string, f kcmdutil.Factory, stream kcmdutil.CheckErr(kcmdutil.UsageErrorf(c, err.Error())) } - if len(kcmdutil.GetFlagString(c, "output")) != 0 { - secret, err := o.NewSSHAuthSecret() - kcmdutil.CheckErr(err) - - kcmdutil.CheckErr(kcmdutil.PrintObject(c, secret, streams.Out)) - return - } - - if err := o.CreateSSHAuthSecret(); err != nil { + if err := o.Run(); err != nil { kcmdutil.CheckErr(err) } }, @@ -96,13 +98,12 @@ func NewCmdCreateSSHAuthSecret(name, fullName string, f kcmdutil.Factory, stream cmd.Flags().StringVar(&o.GitConfigPath, "gitconfig", "", "Path to a .gitconfig file") cmd.MarkFlagFilename("gitconfig") - kcmdutil.AddPrinterFlags(cmd) - + o.PrintFlags.AddFlags(cmd) return cmd } // CreateSSHAuthSecret saves created Secret structure and prints the secret name to the output on success. -func (o *CreateSSHAuthSecretOptions) CreateSSHAuthSecret() error { +func (o *CreateSSHAuthSecretOptions) Run() error { secret, err := o.NewSSHAuthSecret() if err != nil { return err @@ -112,16 +113,15 @@ func (o *CreateSSHAuthSecretOptions) CreateSSHAuthSecret() error { return err } - fmt.Fprintf(o.GetOut(), "secret/%s\n", secret.Name) - return nil + return o.Printer.PrintObj(secret, o.Out) } // NewSSHAuthSecret builds up the Secret structure containing secret name, type and data structure // containing desired credentials. -func (o *CreateSSHAuthSecretOptions) NewSSHAuthSecret() (*api.Secret, error) { - secret := &api.Secret{} +func (o *CreateSSHAuthSecretOptions) NewSSHAuthSecret() (*coreapiv1.Secret, error) { + secret := &coreapiv1.Secret{} secret.Name = o.SecretName - secret.Type = api.SecretTypeSSHAuth + secret.Type = coreapiv1.SecretTypeSSHAuth secret.Data = map[string][]byte{} if len(o.PrivateKeyPath) != 0 { @@ -159,20 +159,23 @@ func (o *CreateSSHAuthSecretOptions) Complete(f kcmdutil.Factory, args []string) } o.SecretName = args[0] - if f != nil { - clientConfig, err := f.ToRESTConfig() - if err != nil { - return err - } - client, err := kcoreclient.NewForConfig(clientConfig) - if err != nil { - return err - } - namespace, _, err := f.ToRawKubeConfigLoader().Namespace() - if err != nil { - return err - } - o.SecretsInterface = client.Secrets(namespace) + clientConfig, err := f.ToRESTConfig() + if err != nil { + return err + } + client, err := coreclientv1.NewForConfig(clientConfig) + if err != nil { + return err + } + namespace, _, err := f.ToRawKubeConfigLoader().Namespace() + if err != nil { + return err + } + o.SecretsInterface = client.Secrets(namespace) + + o.Printer, err = o.PrintFlags.ToPrinter() + if err != nil { + return err } return nil @@ -190,13 +193,3 @@ func (o CreateSSHAuthSecretOptions) Validate() error { return nil } - -// GetOut check if the CreateSSHAuthSecretOptions Out Writer is set. Returns it if the Writer -// is present, if not returns Writer on which all Write calls succeed without doing anything. -func (o CreateSSHAuthSecretOptions) GetOut() io.Writer { - if o.Out == nil { - return ioutil.Discard - } - - return o.Out -} diff --git a/pkg/oc/cli/secrets/unlink_secret_from_object.go b/pkg/oc/cli/secrets/unlink_secret_from_object.go index 14ba4e626d3c..df5946f4e57a 100644 --- a/pkg/oc/cli/secrets/unlink_secret_from_object.go +++ b/pkg/oc/cli/secrets/unlink_secret_from_object.go @@ -4,12 +4,14 @@ import ( "errors" "fmt" - kapi "k8s.io/kubernetes/pkg/apis/core" + "github.com/spf13/cobra" + + "github.com/openshift/origin/pkg/oc/util/ocscheme" + coreapiv1 "k8s.io/api/core/v1" "k8s.io/kubernetes/pkg/kubectl/cmd/templates" kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/kubectl/genericclioptions" - - "github.com/spf13/cobra" + "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers" ) const UnlinkSecretRecommendedName = "unlink" @@ -27,11 +29,24 @@ var ( type UnlinkSecretOptions struct { SecretOptions + + PrintFlags *genericclioptions.PrintFlags + Printer printers.ResourcePrinter + + genericclioptions.IOStreams +} + +func NewUnlinkSecretOptions(streams genericclioptions.IOStreams) *UnlinkSecretOptions { + return &UnlinkSecretOptions{ + PrintFlags: genericclioptions.NewPrintFlags("updated").WithTypeSetter(ocscheme.PrintingInternalScheme), + SecretOptions: SecretOptions{}, + IOStreams: streams, + } } // NewCmdUnlinkSecret creates a command object for detaching one or more secret references from a service account func NewCmdUnlinkSecret(name, fullName string, f kcmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command { - o := &UnlinkSecretOptions{SecretOptions{Out: streams.Out}} + o := NewUnlinkSecretOptions(streams) cmd := &cobra.Command{ Use: fmt.Sprintf("%s serviceaccount-name secret-name [another-secret-name] ...", name), @@ -46,17 +61,32 @@ func NewCmdUnlinkSecret(name, fullName string, f kcmdutil.Factory, streams gener kcmdutil.CheckErr(kcmdutil.UsageErrorf(c, err.Error())) } - if err := o.UnlinkSecrets(); err != nil { + if err := o.Run(); err != nil { kcmdutil.CheckErr(err) } }, } + o.PrintFlags.AddFlags(cmd) return cmd } -func (o UnlinkSecretOptions) UnlinkSecrets() error { +func (o *UnlinkSecretOptions) Complete(f kcmdutil.Factory, args []string) error { + if err := o.SecretOptions.Complete(f, args); err != nil { + return err + } + + var err error + o.Printer, err = o.PrintFlags.ToPrinter() + if err != nil { + return err + } + + return nil +} + +func (o UnlinkSecretOptions) Run() error { serviceaccount, err := o.GetServiceAccount() if err != nil { return err @@ -66,11 +96,11 @@ func (o UnlinkSecretOptions) UnlinkSecrets() error { return err } - return nil + return o.Printer.PrintObj(serviceaccount, o.Out) } // unlinkSecretsFromServiceAccount detaches pull and mount secrets from the service account. -func (o UnlinkSecretOptions) unlinkSecretsFromServiceAccount(serviceaccount *kapi.ServiceAccount) error { +func (o UnlinkSecretOptions) unlinkSecretsFromServiceAccount(serviceaccount *coreapiv1.ServiceAccount) error { // All of the requested secrets must be present in either the Mount or Pull secrets // If any of them are not present, we'll return an error and push no changes. rmSecrets, hasNotFound, err := o.GetSecrets(true) @@ -79,8 +109,8 @@ func (o UnlinkSecretOptions) unlinkSecretsFromServiceAccount(serviceaccount *kap } rmSecretNames := o.GetSecretNames(rmSecrets) - newMountSecrets := []kapi.ObjectReference{} - newPullSecrets := []kapi.LocalObjectReference{} + newMountSecrets := []coreapiv1.ObjectReference{} + newPullSecrets := []coreapiv1.LocalObjectReference{} updated := false // Check the mount secrets