Skip to content

Commit 640dce3

Browse files
committed
doc/design/milestone-0.0.7/csv-generation.md: explanation of and example code for 'CSVUpdater' interface
1 parent 69b71cb commit 640dce3

File tree

1 file changed

+76
-6
lines changed

1 file changed

+76
-6
lines changed

doc/design/milestone-0.0.7/csv-generation.md

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,80 @@ type CSVUpdater interface {
4242
}
4343
```
4444

45-
`Apply` will use data from the underlying implementer of the `CSVUpdater` interface to operate on `*v1alpha1.ClusterServiceVersion` instance fields relevant to that updater. The OLM defines the entire CSV spec [in Golang][olm-code-csv-spec] as separate `struct` components, each of which we can alias to implement `CSVUpdater`. Once sub-step 2 is reached when creating or updating a CSV, `renderCSV` will extract each yaml document discovered, and pass document data into a dispatcher function. The dispatcher selects the correct `CSVUpdater` to populate based on the documents' `Kind` yaml object, a manifest type identifier used in all (TODO: verify) operator manifests.
45+
`Apply` will use data from the underlying implementer of the `CSVUpdater` interface to operate on `*v1alpha1.ClusterServiceVersion` instance fields relevant to that updater. The OLM defines the entire CSV spec [in Golang][olm-code-csv-spec] as a (versioned) hierarchy of `struct` components, each of which we can alias to implement `CSVUpdater`. Once sub-step 2 is reached when creating or updating a CSV, `renderCSV` will extract each yaml document discovered, and pass document data into a dispatcher function. The dispatcher selects the correct `CSVUpdater` to populate based on the documents' `Kind` yaml object, a manifest type identifier used in all (*TODO*: verify) operator manifests. We access the latest version of CSV format and maintain OLM compatibility by leveraging the OLM spec implementations, rather than implementing the spec locally. The following is an example implementation of an [install strategy][olm-csv-install-strat-doc] `CSVUpdater`:
46+
47+
```Go
48+
import (
49+
...
50+
appsv1 "k8s.io/api/apps/v1"
51+
rbacv1beta1 "k8s.io/api/rbac/v1beta1"
52+
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
53+
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install"
54+
)
55+
56+
// CSVInstallStrategyUpdate embeds the OLM's install strategy spec.
57+
type CSVInstallStrategyUpdate struct {
58+
*install.StrategyDetailsDeployment
59+
60+
// Future utility fields go here.
61+
}
62+
63+
// getLocalInstallStrategyCache retrieves the local cache singleton and returns the install strategy cache.
64+
func getLocalInstallStrategyCache() *CSVInstallStrategyUpdate {
65+
factory := getLocalCacheFactory()
66+
return factory.InstallStrategyCache
67+
}
68+
69+
// AddDeploymentSpecToCSVInstallStrategyUpdate adds an RBAC Role to the local cache singletons' permissions.
70+
func AddRoleToCSVInstallStrategyUpdate(yamlDoc []byte) error {
71+
localInstallStrategyUpdate := getLocalInstallStrategyCache()
72+
73+
newRBACRole := new(rbacv1beta1.Role)
74+
_ = yaml.Unmarshal(yamlDoc, newRBACRole)
75+
76+
newPermissions := install.StrategyDeploymentPermissions{
77+
ServiceAccountName: newRole.ObjectMeta.Name,
78+
Rules: newRole.Rules,
79+
}
80+
localInstallStrategyUpdate.Permissions = append(localInstallStrategyUpdate.Permissions, newPermissions)
81+
82+
return nil
83+
}
84+
85+
// AddDeploymentSpecToCSVInstallStrategyUpdate adds a Deployment to the local cache singletons' install strategy.
86+
func AddDeploymentSpecToCSVInstallStrategyUpdate(yamlDoc []byte) error {
87+
localInstallStrategyUpdate := getLocalInstallStrategyCache()
88+
89+
newDeployment := new(appsv1.Deployment)
90+
_ = yaml.Unmarshal(yamlDoc, newDeployment)
91+
92+
newDeploymentSpec := install.StrategyDeploymentSpec{
93+
Name: newDeployment.ObjectMeta.Name,
94+
Spec: newDeployment.Spec,
95+
}
96+
localInstallStrategyUpdate.DeploymentSpecs = append(localInstallStrategyUpdate.DeploymentSpecs, newDeploymentSpec)
97+
98+
return nil
99+
}
100+
101+
// Apply applies cached updates in CSVInstallStrategyUpdate to the appropriate csv fields.
102+
func (us *CSVInstallStrategyUpdate) Apply(csv *v1alpha1.ClusterServiceVersion) error {
103+
// Get install strategy from csv.
104+
var resolver *install.StrategyResolver
105+
strat, _ := resolver.UnmarshalStrategy(csv.Spec.InstallStrategy)
106+
installStrat, _ := strat.(*install.StrategyDetailsDeployment)
107+
108+
// Update permissions and deployments with custom field update methods.
109+
us.updatePermissions(installStrat)
110+
us.updateDeploymentSpecs(installStrat)
111+
112+
// Re-serialize permissions into csv install strategy.
113+
updatedStrat, _ := json.Marshal(installStrat)
114+
csv.Spec.InstallStrategy.StrategySpecRaw = updatedStrat
115+
116+
return nil
117+
}
118+
```
46119

47120
### User-defined yaml objects
48121

@@ -106,11 +179,7 @@ customresourcedefinitions:
106179
...
107180
```
108181

109-
Each of the above yaml objects corresponds to homonymous `spec` object children described by the CSV [`spec` documentation][olm-csv-spec-doc]. [Here][describe-yaml-example] is an example `describe.yaml` a Prometheus operator developer would write to assist in CSV generation.
110-
111-
## API
112-
113-
## Reference
182+
Each of the above yaml objects corresponds to homonymous `spec` object children described by the CSV [`spec` documentation][olm-csv-spec-doc]. [Here][describe-yaml-example] is a workable, complex example `describe.yaml` a Prometheus operator developer would write to assist the SDK in CSV generation.
114183

115184

116185
[olm-csv-definition]:https://github.com/operator-framework/operator-lifecycle-manager/blob/master/Documentation/design/building-your-csv.md#what-is-a-cluster-service-version-csv
@@ -119,4 +188,5 @@ Each of the above yaml objects corresponds to homonymous `spec` object children
119188
[olm-code-csv-struct]:https://github.com/operator-framework/operator-lifecycle-manager/blob/8799f39ef342dc1ff7430eba7a88c1c3c70cbdcc/pkg/api/apis/operators/v1alpha1/clusterserviceversion_types.go#L261
120189
[olm-code-csv-spec]:https://github.com/operator-framework/operator-lifecycle-manager/blob/8799f39ef342dc1ff7430eba7a88c1c3c70cbdcc/pkg/api/apis/operators/v1alpha1/clusterserviceversion_types.go
121190
[olm-csv-spec-doc]:https://github.com/operator-framework/operator-lifecycle-manager/blob/16ff8f983b50503c4d8b8015bd0c14b5c7d6786a/Documentation/design/building-your-csv.md#building-a-cluster-service-version-csv-for-the-operator-framework
191+
[olm-csv-install-strat-doc]:https://github.com/operator-framework/operator-lifecycle-manager/blob/16ff8f983b50503c4d8b8015bd0c14b5c7d6786a/Documentation/design/building-your-csv.md#operator-install
122192
[describe-yaml-example]:example.describe.yaml

0 commit comments

Comments
 (0)