|
| 1 | +# Open Cluster Management API |
| 2 | + |
| 3 | +The canonical source for all Open Cluster Management (OCM) API definitions, client libraries, and CRDs for multicluster and multicloud Kubernetes scenarios. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +**Type**: Go API Library / Kubernetes CRDs |
| 8 | +**Language**: Go |
| 9 | +**Framework**: Kubernetes API Machinery / controller-runtime |
| 10 | +**Primary Purpose**: API definitions and client libraries for OCM ecosystem |
| 11 | +**Target Users**: Platform engineers, multicluster operators, OCM addon developers |
| 12 | + |
| 13 | +## Technical Stack |
| 14 | + |
| 15 | +- **Language**: Go 1.23+ with modern module structure |
| 16 | +- **Kubernetes Integration**: API Machinery for CRD definitions |
| 17 | +- **Code Generation**: Kubernetes code generators for clients and deepcopy |
| 18 | +- **API Versioning**: Multiple API versions (v1, v1beta1, v1alpha1) |
| 19 | +- **Client Libraries**: Kubernetes-style typed clientsets |
| 20 | +- **Validation**: OpenAPI schema generation for CRD validation |
| 21 | + |
| 22 | +## API Architecture |
| 23 | + |
| 24 | +### Four Core API Groups |
| 25 | + |
| 26 | +#### 1. Cluster Management (`cluster.open-cluster-management.io`) |
| 27 | +- **v1**: Core cluster registration and grouping |
| 28 | + - `ManagedCluster`: Represents a cluster joined to the hub |
| 29 | + - `ManagedClusterSet`: Logical grouping of clusters |
| 30 | + - `ManagedClusterSetBinding`: Namespace-level cluster set access |
| 31 | +- **v1beta1**: Advanced cluster selection and placement |
| 32 | + - `Placement`: Intelligent cluster selection with constraints |
| 33 | + - `PlacementDecision`: Results of placement decisions |
| 34 | +- **v1alpha1**: Extended cluster capabilities |
| 35 | + - `ClusterClaim`: Cluster-specific capability declarations |
| 36 | + - `AddonPlacementScore`: Scoring for placement decisions |
| 37 | + |
| 38 | +#### 2. Work Distribution (`work.open-cluster-management.io`) |
| 39 | +- **v1**: Resource deployment across clusters |
| 40 | + - `ManifestWork`: Kubernetes resources to deploy on managed clusters |
| 41 | + - `AppliedManifestWork`: Status of deployed resources |
| 42 | +- **v1alpha1**: Bulk deployment capabilities |
| 43 | + - `ManifestWorkReplicaSet`: Deploy to multiple clusters in a set |
| 44 | + |
| 45 | +#### 3. Addon Management (`addon.open-cluster-management.io`) |
| 46 | +- **v1alpha1**: Addon lifecycle and configuration |
| 47 | + - `ClusterManagementAddOn`: Hub-side addon definition |
| 48 | + - `ManagedClusterAddOn`: Cluster-specific addon instance |
| 49 | + - `AddonDeploymentConfig`: Addon configuration templates |
| 50 | + |
| 51 | +#### 4. Operator APIs (`operator.open-cluster-management.io`) |
| 52 | +- **v1**: OCM component installation |
| 53 | + - `ClusterManager`: Hub cluster OCM installation |
| 54 | + - `Klusterlet`: Managed cluster agent installation |
| 55 | + |
| 56 | +## Development Workflow |
| 57 | + |
| 58 | +### Code Generation |
| 59 | +```bash |
| 60 | +# Update all generated code (clients, deepcopy, CRDs) |
| 61 | +make update |
| 62 | + |
| 63 | +# Verify code generation is up to date |
| 64 | +make verify |
| 65 | + |
| 66 | +# Run all tests |
| 67 | +make test |
| 68 | +``` |
| 69 | + |
| 70 | +### Generated Components |
| 71 | +- **DeepCopy Methods**: For all API struct types |
| 72 | +- **Typed Clients**: Kubernetes-style clientsets for each API group |
| 73 | +- **Informers/Listers**: Efficient caching and watching mechanisms |
| 74 | +- **OpenAPI Schemas**: CRD validation and documentation |
| 75 | + |
| 76 | +## AI Development Guidelines |
| 77 | + |
| 78 | +### API Design Patterns |
| 79 | + |
| 80 | +#### Resource Structure |
| 81 | +```go |
| 82 | +// Standard Kubernetes API resource pattern |
| 83 | +type ManagedCluster struct { |
| 84 | + metav1.TypeMeta `json:",inline"` |
| 85 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 86 | + |
| 87 | + Spec ManagedClusterSpec `json:"spec,omitempty"` |
| 88 | + Status ManagedClusterStatus `json:"status,omitempty"` |
| 89 | +} |
| 90 | + |
| 91 | +// +kubebuilder:object:root=true |
| 92 | +// +kubebuilder:resource:scope=Cluster |
| 93 | +``` |
| 94 | + |
| 95 | +#### Client Usage Patterns |
| 96 | +```go |
| 97 | +import ( |
| 98 | + clusterv1 "open-cluster-management.io/api/cluster/v1" |
| 99 | + clusterclient "open-cluster-management.io/api/client/cluster/clientset/versioned" |
| 100 | +) |
| 101 | + |
| 102 | +// Create typed client |
| 103 | +config, err := ctrl.GetConfig() |
| 104 | +client := clusterclient.NewForConfigOrDie(config) |
| 105 | + |
| 106 | +// List managed clusters |
| 107 | +clusters, err := client.ClusterV1().ManagedClusters().List(ctx, metav1.ListOptions{}) |
| 108 | + |
| 109 | +// Watch for changes |
| 110 | +watchInterface, err := client.ClusterV1().ManagedClusters().Watch(ctx, metav1.ListOptions{}) |
| 111 | +``` |
| 112 | + |
| 113 | +#### Status Management |
| 114 | +```go |
| 115 | +// Standard status update pattern |
| 116 | +managedCluster.Status.Conditions = []metav1.Condition{ |
| 117 | + { |
| 118 | + Type: "ManagedClusterJoined", |
| 119 | + Status: metav1.ConditionTrue, |
| 120 | + Reason: "ManagedClusterJoined", |
| 121 | + Message: "Cluster successfully joined the hub", |
| 122 | + }, |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +### Common Development Tasks |
| 127 | + |
| 128 | +#### Adding New API Types |
| 129 | +1. **Define Structure**: Create Go structs with proper kubebuilder tags |
| 130 | +2. **Register Types**: Add to scheme registration in `register.go` |
| 131 | +3. **Generate Code**: Run `make update` to generate clients and deepcopy |
| 132 | +4. **Add Tests**: Create comprehensive unit tests for new types |
| 133 | +5. **Update Documentation**: Add examples and usage patterns |
| 134 | + |
| 135 | +#### Working with Placements |
| 136 | +```go |
| 137 | +// Create placement with cluster selection |
| 138 | +placement := &clusterv1beta1.Placement{ |
| 139 | + ObjectMeta: metav1.ObjectMeta{ |
| 140 | + Name: "my-placement", |
| 141 | + Namespace: "default", |
| 142 | + }, |
| 143 | + Spec: clusterv1beta1.PlacementSpec{ |
| 144 | + ClusterSets: []string{"clusterset1"}, |
| 145 | + Predicates: []clusterv1beta1.ClusterPredicate{ |
| 146 | + { |
| 147 | + RequiredClusterSelector: clusterv1beta1.ClusterSelector{ |
| 148 | + LabelSelector: metav1.LabelSelector{ |
| 149 | + MatchLabels: map[string]string{ |
| 150 | + "environment": "production", |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +#### ManifestWork Deployment |
| 161 | +```go |
| 162 | +// Deploy resources to managed clusters |
| 163 | +manifestWork := &workv1.ManifestWork{ |
| 164 | + ObjectMeta: metav1.ObjectMeta{ |
| 165 | + Name: "my-workload", |
| 166 | + Namespace: "cluster1", // managed cluster namespace |
| 167 | + }, |
| 168 | + Spec: workv1.ManifestWorkSpec{ |
| 169 | + Workload: workv1.ManifestsTemplate{ |
| 170 | + Manifests: []workv1.Manifest{ |
| 171 | + { |
| 172 | + Object: unstructured.Unstructured{ |
| 173 | + Object: map[string]interface{}{ |
| 174 | + "apiVersion": "apps/v1", |
| 175 | + "kind": "Deployment", |
| 176 | + "metadata": map[string]interface{}{ |
| 177 | + "name": "my-app", |
| 178 | + "namespace": "default", |
| 179 | + }, |
| 180 | + // ... deployment spec |
| 181 | + }, |
| 182 | + }, |
| 183 | + }, |
| 184 | + }, |
| 185 | + }, |
| 186 | + }, |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +### Integration Patterns |
| 191 | + |
| 192 | +#### Controller Development |
| 193 | +```go |
| 194 | +// Standard controller setup for OCM APIs |
| 195 | +import ( |
| 196 | + clusterv1 "open-cluster-management.io/api/cluster/v1" |
| 197 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 198 | +) |
| 199 | + |
| 200 | +func (r *MyReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 201 | + return ctrl.NewControllerManagedBy(mgr). |
| 202 | + For(&clusterv1.ManagedCluster{}). |
| 203 | + Watches(&workv1.ManifestWork{}, &handler.EnqueueRequestForObject{}). |
| 204 | + Complete(r) |
| 205 | +} |
| 206 | +``` |
| 207 | + |
| 208 | +#### Informer Usage |
| 209 | +```go |
| 210 | +// Use generated informers for efficient watching |
| 211 | +import ( |
| 212 | + clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions" |
| 213 | +) |
| 214 | + |
| 215 | +informerFactory := clusterinformers.NewSharedInformerFactory(client, time.Minute*30) |
| 216 | +clusterInformer := informerFactory.Cluster().V1().ManagedClusters() |
| 217 | + |
| 218 | +clusterInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{ |
| 219 | + AddFunc: handleClusterAdd, |
| 220 | + UpdateFunc: handleClusterUpdate, |
| 221 | + DeleteFunc: handleClusterDelete, |
| 222 | +}) |
| 223 | +``` |
| 224 | + |
| 225 | +### Best Practices |
| 226 | + |
| 227 | +#### API Versioning |
| 228 | +- **v1alpha1**: Experimental APIs, may have breaking changes |
| 229 | +- **v1beta1**: Stable APIs, backward compatible changes only |
| 230 | +- **v1**: Stable APIs, no breaking changes allowed |
| 231 | +- Use feature gates for experimental functionality |
| 232 | + |
| 233 | +#### Resource Naming |
| 234 | +- Follow Kubernetes naming conventions |
| 235 | +- Use descriptive, action-oriented names |
| 236 | +- Maintain consistency across API groups |
| 237 | +- Consider plural/singular forms carefully |
| 238 | + |
| 239 | +#### Status Conventions |
| 240 | +```go |
| 241 | +// Use standard Kubernetes condition types |
| 242 | +const ( |
| 243 | + ConditionReady = "Ready" |
| 244 | + ConditionAvailable = "Available" |
| 245 | + ConditionDegraded = "Degraded" |
| 246 | +) |
| 247 | + |
| 248 | +// Include helpful error messages |
| 249 | +condition := metav1.Condition{ |
| 250 | + Type: ConditionReady, |
| 251 | + Status: metav1.ConditionFalse, |
| 252 | + Reason: "InvalidConfiguration", |
| 253 | + Message: "Cluster configuration validation failed: missing required labels", |
| 254 | + LastTransitionTime: metav1.Now(), |
| 255 | +} |
| 256 | +``` |
| 257 | + |
| 258 | +#### Validation and Defaults |
| 259 | +```go |
| 260 | +// Use kubebuilder tags for validation |
| 261 | +type ManagedClusterSpec struct { |
| 262 | + // +kubebuilder:validation:Required |
| 263 | + // +kubebuilder:default=true |
| 264 | + HubAcceptsClient bool `json:"hubAcceptsClient"` |
| 265 | + |
| 266 | + // +kubebuilder:validation:Pattern=`^[a-z0-9]([-a-z0-9]*[a-z0-9])?$` |
| 267 | + LeaseDurationSeconds *int32 `json:"leaseDurationSeconds,omitempty"` |
| 268 | +} |
| 269 | +``` |
| 270 | + |
| 271 | +## Useful Commands |
| 272 | + |
| 273 | +```bash |
| 274 | +# Code Generation |
| 275 | +make update # Generate all code (clients, deepcopy, etc.) |
| 276 | +make verify # Verify generated code is up to date |
| 277 | +make clean # Clean generated code |
| 278 | + |
| 279 | +# Testing |
| 280 | +make test # Run unit tests |
| 281 | +make test-integration # Run integration tests |
| 282 | +make verify-crds # Verify CRD manifests |
| 283 | + |
| 284 | +# Development |
| 285 | +make fmt # Format Go code |
| 286 | +make vet # Run go vet |
| 287 | +make lint # Run linters |
| 288 | +``` |
| 289 | + |
| 290 | +## Integration Examples |
| 291 | + |
| 292 | +### External Project Integration |
| 293 | +```go |
| 294 | +// go.mod |
| 295 | +module my-ocm-controller |
| 296 | + |
| 297 | +require ( |
| 298 | + open-cluster-management.io/api v0.13.0 |
| 299 | + sigs.k8s.io/controller-runtime v0.16.0 |
| 300 | +) |
| 301 | + |
| 302 | +// main.go |
| 303 | +import ( |
| 304 | + clusterv1 "open-cluster-management.io/api/cluster/v1" |
| 305 | + clusterscheme "open-cluster-management.io/api/client/cluster/clientset/versioned/scheme" |
| 306 | +) |
| 307 | + |
| 308 | +// Add OCM APIs to your scheme |
| 309 | +scheme := runtime.NewScheme() |
| 310 | +clusterscheme.AddToScheme(scheme) |
| 311 | +``` |
| 312 | + |
| 313 | +### CRD Installation |
| 314 | +```bash |
| 315 | +# Install CRDs from this repository |
| 316 | +kubectl apply -f https://raw.githubusercontent.com/open-cluster-management-io/api/main/cluster/v1/0000_00_clusters.open-cluster-management.io_managedclusters.crd.yaml |
| 317 | +``` |
| 318 | + |
| 319 | +## Project Structure |
| 320 | + |
| 321 | +``` |
| 322 | +├── addon/ # Addon management API definitions |
| 323 | +│ └── v1alpha1/ # Addon API types and client code |
| 324 | +├── cluster/ # Cluster management API definitions |
| 325 | +│ ├── v1/ # Stable cluster APIs |
| 326 | +│ ├── v1beta1/ # Beta placement APIs |
| 327 | +│ └── v1alpha1/ # Alpha cluster capabilities APIs |
| 328 | +├── work/ # Work distribution API definitions |
| 329 | +│ ├── v1/ # Stable work APIs |
| 330 | +│ └── v1alpha1/ # Alpha work APIs |
| 331 | +├── operator/ # Operator installation APIs |
| 332 | +│ └── v1/ # Operator APIs |
| 333 | +├── client/ # Generated Kubernetes clients |
| 334 | +│ ├── addon/ # Addon client packages |
| 335 | +│ ├── cluster/ # Cluster client packages |
| 336 | +│ ├── work/ # Work client packages |
| 337 | +│ └── operator/ # Operator client packages |
| 338 | +└── utils/ # Utility libraries and helpers |
| 339 | +``` |
| 340 | + |
| 341 | +## Contributing Guidelines |
| 342 | + |
| 343 | +- **API Compatibility**: Maintain backward compatibility for stable APIs |
| 344 | +- **Code Generation**: Always run `make update` after API changes |
| 345 | +- **Testing**: Comprehensive unit tests for all API types |
| 346 | +- **Documentation**: Update examples and API documentation |
| 347 | +- **Validation**: Add appropriate kubebuilder validation tags |
| 348 | +- **Versioning**: Follow Kubernetes API versioning guidelines |
| 349 | + |
| 350 | +This repository serves as the foundation for the entire Open Cluster Management ecosystem, enabling multicluster Kubernetes management at scale. |
0 commit comments