2017-02-08 17:42:31 +00:00
/ *
Copyright 2017 The Kubernetes Authors .
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 .
* /
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"io"
"github.com/ghodss/yaml"
"github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
2017-07-07 04:04:11 +00:00
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
2017-02-08 17:42:31 +00:00
)
type ViewLastAppliedOptions struct {
FilenameOptions resource . FilenameOptions
Selector string
LastAppliedConfigurationList [ ] string
OutputFormat string
2017-04-07 13:04:55 +00:00
All bool
2017-02-08 17:42:31 +00:00
Factory cmdutil . Factory
Out io . Writer
ErrOut io . Writer
}
var (
2017-03-15 03:49:10 +00:00
applyViewLastAppliedLong = templates . LongDesc ( i18n . T ( `
2017-02-08 17:42:31 +00:00
View the latest last - applied - configuration annotations by type / name or file .
The default output will be printed to stdout in YAML format . One can use - o option
2017-03-15 03:49:10 +00:00
to change output format . ` ) )
2017-02-08 17:42:31 +00:00
2017-03-15 03:49:10 +00:00
applyViewLastAppliedExample = templates . Examples ( i18n . T ( `
2017-02-08 17:42:31 +00:00
# View the last - applied - configuration annotations by type / name in YAML .
kubectl apply view - last - applied deployment / nginx
# View the last - applied - configuration annotations by file in JSON
2017-03-15 03:49:10 +00:00
kubectl apply view - last - applied - f deploy . yaml - o json ` ) )
2017-02-08 17:42:31 +00:00
)
func NewCmdApplyViewLastApplied ( f cmdutil . Factory , out , err io . Writer ) * cobra . Command {
options := & ViewLastAppliedOptions { Out : out , ErrOut : err }
cmd := & cobra . Command {
Use : "view-last-applied (TYPE [NAME | -l label] | TYPE/NAME | -f FILENAME)" ,
2017-03-15 03:49:10 +00:00
Short : i18n . T ( "View latest last-applied-configuration annotations of a resource/object" ) ,
2017-02-08 17:42:31 +00:00
Long : applyViewLastAppliedLong ,
Example : applyViewLastAppliedExample ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
cmdutil . CheckErr ( options . ValidateOutputArgs ( cmd ) )
cmdutil . CheckErr ( options . Complete ( f , args ) )
cmdutil . CheckErr ( options . Validate ( cmd ) )
cmdutil . CheckErr ( options . RunApplyViewLastApplied ( ) )
} ,
}
cmd . Flags ( ) . StringP ( "output" , "o" , "" , "Output format. Must be one of yaml|json" )
2017-08-17 12:27:11 +00:00
cmd . Flags ( ) . StringVarP ( & options . Selector , "selector" , "l" , "" , "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)" )
2017-08-02 15:41:19 +00:00
cmd . Flags ( ) . BoolVar ( & options . All , "all" , false , "Select all resources in the namespace of the specified resource types" )
2017-02-08 17:42:31 +00:00
usage := "that contains the last-applied-configuration annotations"
cmdutil . AddFilenameOptionFlags ( cmd , & options . FilenameOptions , usage )
return cmd
}
func ( o * ViewLastAppliedOptions ) Complete ( f cmdutil . Factory , args [ ] string ) error {
2017-05-16 21:52:51 +00:00
cmdNamespace , enforceNamespace , err := f . DefaultNamespace ( )
2017-02-08 17:42:31 +00:00
if err != nil {
return err
}
2017-08-02 20:23:07 +00:00
mapper , typer , err := f . UnstructuredObject ( )
2017-02-08 17:42:31 +00:00
if err != nil {
return err
}
2017-08-02 20:23:07 +00:00
r := f . NewBuilder ( ) .
Unstructured ( f . UnstructuredClientForMapping , mapper , typer ) .
2017-02-08 17:42:31 +00:00
NamespaceParam ( cmdNamespace ) . DefaultNamespace ( ) .
FilenameParam ( enforceNamespace , & o . FilenameOptions ) .
ResourceTypeOrNameArgs ( enforceNamespace , args ... ) .
2017-04-07 13:04:55 +00:00
SelectAllParam ( o . All ) .
2017-02-08 17:42:31 +00:00
SelectorParam ( o . Selector ) .
Latest ( ) .
Flatten ( ) .
Do ( )
err = r . Err ( )
if err != nil {
return err
}
err = r . Visit ( func ( info * resource . Info , err error ) error {
if err != nil {
return err
}
configString , err := kubectl . GetOriginalConfiguration ( info . Mapping , info . Object )
if err != nil {
return err
}
if configString == nil {
return cmdutil . AddSourceToErr ( fmt . Sprintf ( "no last-applied-configuration annotation found on resource: %s\n" , info . Name ) , info . Source , err )
}
o . LastAppliedConfigurationList = append ( o . LastAppliedConfigurationList , string ( configString ) )
return nil
} )
if err != nil {
return err
}
return nil
}
func ( o * ViewLastAppliedOptions ) Validate ( cmd * cobra . Command ) error {
return nil
}
func ( o * ViewLastAppliedOptions ) RunApplyViewLastApplied ( ) error {
for _ , str := range o . LastAppliedConfigurationList {
switch o . OutputFormat {
case "json" :
jsonBuffer := & bytes . Buffer { }
2017-03-31 00:47:18 +00:00
err := json . Indent ( jsonBuffer , [ ] byte ( str ) , "" , " " )
2017-02-08 17:42:31 +00:00
if err != nil {
return err
}
2017-05-10 20:17:12 +00:00
fmt . Fprint ( o . Out , string ( jsonBuffer . Bytes ( ) ) )
2017-02-08 17:42:31 +00:00
case "yaml" :
2017-03-31 00:47:18 +00:00
yamlOutput , err := yaml . JSONToYAML ( [ ] byte ( str ) )
if err != nil {
return err
}
2017-05-10 20:17:12 +00:00
fmt . Fprint ( o . Out , string ( yamlOutput ) )
2017-02-08 17:42:31 +00:00
}
}
return nil
}
func ( o * ViewLastAppliedOptions ) ValidateOutputArgs ( cmd * cobra . Command ) error {
format := cmdutil . GetFlagString ( cmd , "output" )
switch format {
case "json" :
o . OutputFormat = "json"
return nil
// If flag -o is not specified, use yaml as default
case "yaml" , "" :
o . OutputFormat = "yaml"
return nil
default :
2017-06-14 21:14:42 +00:00
return cmdutil . UsageErrorf ( cmd , "Unexpected -o output mode: %s, the flag 'output' must be one of yaml|json" , format )
2017-02-08 17:42:31 +00:00
}
}