Moves visitedUids and visitedNamespaces (used for pruning) into ApplyOptions

Kubernetes-commit: 4e45421f610eb881a03c0d82fca038788db12b08
This commit is contained in:
Sean Sullivan 2019-12-17 14:19:00 -08:00 committed by Kubernetes Publisher
parent 42da131431
commit 641631dfad
2 changed files with 47 additions and 19 deletions

View File

@ -92,6 +92,19 @@ type ApplyOptions struct {
// not call the resource builder; only return the set objects. // not call the resource builder; only return the set objects.
objects []*resource.Info objects []*resource.Info
objectsCached bool objectsCached bool
// Stores visited objects/namespaces for later use
// calculating the set of objects to prune.
VisitedUids sets.String
VisitedNamespaces sets.String
// Function run after the objects are generated and
// stored in the "objects" field, but before the
// apply is run on these objects.
preProcessorFn func(*ApplyOptions) error
// Function run after all objects have been applied.
// The standard postprocessorFn is "PrintAndPrune()".
postProcessorFn func(*ApplyOptions) error
} }
var ( var (
@ -140,6 +153,11 @@ func NewApplyOptions(ioStreams genericclioptions.IOStreams) *ApplyOptions {
objects: []*resource.Info{}, objects: []*resource.Info{},
objectsCached: false, objectsCached: false,
VisitedUids: sets.NewString(),
VisitedNamespaces: sets.NewString(),
postProcessorFn: PrintAndPrune,
} }
} }
@ -340,9 +358,6 @@ func (o *ApplyOptions) Run() error {
OpenAPIGetter: o.DiscoveryClient, OpenAPIGetter: o.DiscoveryClient,
} }
visitedUids := sets.NewString()
visitedNamespaces := sets.NewString()
infos, err := o.GetObjects() infos, err := o.GetObjects()
if err != nil { if err != nil {
return err return err
@ -360,9 +375,7 @@ func (o *ApplyOptions) Run() error {
} }
} }
if info.Namespaced() { o.MarkNamespaceVisited(info)
visitedNamespaces.Insert(info.Namespace)
}
if err := o.Recorder.Record(info.Object); err != nil { if err := o.Recorder.Record(info.Object); err != nil {
klog.V(4).Infof("error recording current command: %v", err) klog.V(4).Infof("error recording current command: %v", err)
@ -414,12 +427,11 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
} }
info.Refresh(obj, true) info.Refresh(obj, true)
metadata, err := meta.Accessor(info.Object)
if err != nil { if err := o.MarkObjectVisited(info); err != nil {
return err return err
} }
visitedUids.Insert(string(metadata.GetUID()))
if o.shouldPrintObject() { if o.shouldPrintObject() {
continue continue
} }
@ -467,11 +479,9 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
info.Refresh(obj, true) info.Refresh(obj, true)
} }
metadata, err := meta.Accessor(info.Object) if err := o.MarkObjectVisited(info); err != nil {
if err != nil {
return err return err
} }
visitedUids.Insert(string(metadata.GetUID()))
if o.shouldPrintObject() { if o.shouldPrintObject() {
continue continue
@ -487,13 +497,12 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
continue continue
} }
metadata, err := meta.Accessor(info.Object) if err := o.MarkObjectVisited(info); err != nil {
if err != nil {
return err return err
} }
visitedUids.Insert(string(metadata.GetUID()))
if !o.DryRun { if !o.DryRun {
metadata, _ := meta.Accessor(info.Object)
annotationMap := metadata.GetAnnotations() annotationMap := metadata.GetAnnotations()
if _, ok := annotationMap[corev1.LastAppliedConfigAnnotation]; !ok { if _, ok := annotationMap[corev1.LastAppliedConfigAnnotation]; !ok {
fmt.Fprintf(o.ErrOut, warningNoLastAppliedConfigAnnotation, o.cmdBaseName) fmt.Fprintf(o.ErrOut, warningNoLastAppliedConfigAnnotation, o.cmdBaseName)
@ -537,7 +546,7 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
} }
if o.Prune { if o.Prune {
p := newPruner(o, visitedUids, visitedNamespaces) p := newPruner(o)
return p.pruneAll(o) return p.pruneAll(o)
} }
@ -599,6 +608,25 @@ func (o *ApplyOptions) printObjects() error {
return nil return nil
} }
// MarkNamespaceVisited keeps track of which namespaces the applied
// objects belong to. Used for pruning.
func (o *ApplyOptions) MarkNamespaceVisited(info *resource.Info) {
if info.Namespaced() {
o.VisitedNamespaces.Insert(info.Namespace)
}
}
// MarkNamespaceVisited keeps track of UIDs of the applied
// objects. Used for pruning.
func (o *ApplyOptions) MarkObjectVisited(info *resource.Info) error {
metadata, err := meta.Accessor(info.Object)
if err != nil {
return err
}
o.VisitedUids.Insert(string(metadata.GetUID()))
return nil
}
// DryRunVerifier verifies if a given group-version-kind supports DryRun // DryRunVerifier verifies if a given group-version-kind supports DryRun
// against the current server. Sending dryRun requests to apiserver that // against the current server. Sending dryRun requests to apiserver that
// don't support it will result in objects being unwillingly persisted. // don't support it will result in objects being unwillingly persisted.

View File

@ -49,14 +49,14 @@ type pruner struct {
out io.Writer out io.Writer
} }
func newPruner(o *ApplyOptions, visitedUids sets.String, visitedNamespaces sets.String) pruner { func newPruner(o *ApplyOptions) pruner {
return pruner{ return pruner{
mapper: o.Mapper, mapper: o.Mapper,
dynamicClient: o.DynamicClient, dynamicClient: o.DynamicClient,
labelSelector: o.Selector, labelSelector: o.Selector,
visitedUids: visitedUids, visitedUids: o.VisitedUids,
visitedNamespaces: visitedNamespaces, visitedNamespaces: o.VisitedNamespaces,
cascade: o.DeleteOptions.Cascade, cascade: o.DeleteOptions.Cascade,
dryRun: o.DryRun, dryRun: o.DryRun,