@@ -49,26 +49,120 @@ func (t ToolingID) String() string {
49
49
return fmt .Sprintf ("%s/%s" , t .Name , t .Version )
50
50
}
51
51
52
+ /*
53
+ The Set interface provides methods for:
54
+ - Add - Add an object to the set
55
+ - Apply - Apply objects in the set to the cluster along with pruning
56
+ - DryRun - Dry run calls the kubernetes API with dryrun flag set to true.
57
+ No actual resources are created or pruned.
58
+
59
+ Add() is used to add object to the apply Set.
60
+ It takes unstructured object.
61
+ It does a get from the cluster to note the resource-version before apply.
62
+
63
+ Apply() method applies the objects in the set to a Kubernetes cluster. If
64
+ the prune parameter is true, any objects that were previously applied but are
65
+ no longer in the set will be deleted from the cluster.
66
+
67
+ DryRun() method can be used to see what changes would be made without actually making them.
68
+
69
+ Example Usage:
70
+
71
+ // Create an ApplySet
72
+ // aset, err := applyset.New(parent, restMapper, dynamicClient, applySetConfig)
73
+
74
+ // Add a ConfigMap to the ApplySet
75
+ // configMap := &unstructured.Unstructured{ ... }
76
+ err = aset.Add(context.TODO(), applyset.ApplyableObject{
77
+ Unstructured: configMap,
78
+ ID: "my-config-map", // optional
79
+ })
80
+ if err != nil {
81
+ log.Fatalf("Failed to add object to ApplySet: %v", err)
82
+ }
83
+
84
+ // Apply the changes to the cluster (or dry-run)
85
+ // To apply:
86
+ result, err := aset.Apply(context.TODO(), true) // true to enable pruning
87
+ // or dry-run:
88
+ // result, err := aset.DryRun(context.TODO(), true)
89
+ if err != nil {
90
+ log.Fatalf("Failed to apply/dry-run ApplySet: %v", err)
91
+ }
92
+
93
+ if result.Errors() != nil {
94
+ fmt.Printf("ApplySet completed with errors: %v\n", result.Errors())
95
+ } else {
96
+ fmt.Println("ApplySet completed successfully (or dry-run successful).")
97
+ }
98
+ */
52
99
type Set interface {
53
100
Add (ctx context.Context , obj ApplyableObject ) error
54
101
Apply (ctx context.Context , prune bool ) (* ApplyResult , error )
55
102
DryRun (ctx context.Context , prune bool ) (* ApplyResult , error )
56
103
}
57
104
58
105
type Config struct {
59
- ToolLabels map [string ]string
106
+ // ToolLabels can be used to inject labels into all resources managed by applyset
107
+ ToolLabels map [string ]string
108
+
109
+ // Provide an identifier which is used as field manager for server side apply
110
+ // https://kubernetes.io/docs/reference/using-api/server-side-apply/#managers
60
111
FieldManager string
61
- ToolingID ToolingID
62
- Log logr.Logger
112
+
113
+ // concats the name and version and adds it as an annotatiuon
114
+ // https://kubernetes.io/docs/reference/using-api/server-side-apply/#managers
115
+ ToolingID ToolingID
116
+
117
+ // Log is used to inject the calling reconciler's logger
118
+ Log logr.Logger
63
119
64
120
// Callbacks
65
- LoadCallback func (obj ApplyableObject , clusterValue * unstructured.Unstructured ) error
66
- AfterApplyCallback func (obj AppliedObject ) error
121
+
122
+ // LoadCallback is called after an object is read from the cluster.
123
+ // This is done as part of Add() interface call.
124
+ LoadCallback func (obj ApplyableObject , clusterValue * unstructured.Unstructured ) error
125
+
126
+ // AfterApplyCallback is called after an object has been applied to the cluster.
127
+ // This callback is passed the return value of the apply call.
128
+ AfterApplyCallback func (obj AppliedObject ) error
129
+
130
+ // BeforePruneCallback is called before an object is pruned from the cluster.
131
+ // This callback is passed the object being pruned.
132
+ // This would not be part of the current applyset by definition.
67
133
BeforePruneCallback func (obj PrunedObject ) error
68
134
}
69
135
70
- // New creates a new ApplySet
71
- // parent object is expected to be the current one existing in the cluster
136
+ /*
137
+ New creates a new ApplySet
138
+ parent object is expected to be the current one existing in the cluster
139
+ Use New() to create an apply Set. This function takes the parent object,
140
+ a RESTMapper, a dynamic client, and a configuration object. The parent
141
+ object is the object that "owns" the set of resources being applied.
142
+
143
+ Example usage:
144
+
145
+ // Set up Kubernetes client and RESTMapper
146
+ // dynamicClient = ...
147
+ // restMapper = ...
148
+
149
+ // Define a parent. The parent should exist in the cluster
150
+ // Can be any Kubernetes resource even a custom resource instance
151
+ parent := &unstructured.Unstructured{ Object: map[string]interface{}{} } }
152
+ parent.SetGroupVersionKind(schema.GroupVersionKind{Group: "example.com", Version: "v1", Kind: "MyCustomResource"})
153
+ parent.SetName("somename")
154
+
155
+ // ApplySet configuration
156
+ applySetConfig := applyset.Config{
157
+ ToolingID: applyset.ToolingID{Name: "my-controller", Version: "v1.0.0"},
158
+ FieldManager: "my-controller",
159
+ Log: logr.Discard(), // Use a real logger in production
160
+ }
161
+
162
+ // Create an ApplySet
163
+ aset, err := applyset.New(parent, restMapper, dynamicClient, applySetConfig)
164
+ // if err != nil { ... }
165
+ */
72
166
func New (
73
167
parent * unstructured.Unstructured ,
74
168
restMapper meta.RESTMapper ,
0 commit comments