@@ -20,6 +20,8 @@ import (
2020 "context"
2121 "encoding/json"
2222
23+ cinderapi "github.com/criticalstack/crit/cmd/cinder/api"
24+ "github.com/criticalstack/machine-api/util"
2325 "github.com/go-logr/logr"
2426 "github.com/go-openapi/spec"
2527 corev1 "k8s.io/api/core/v1"
@@ -36,8 +38,6 @@ import (
3638 "github.com/criticalstack/machine-api-provider-docker/api/v1alpha1"
3739)
3840
39- const OpenAPISchemaSecretName = "config-schema"
40-
4141// DockerMachineReconciler reconciles a DockerMachine object
4242type DockerInfrastructureProviderReconciler struct {
4343 client.Client
@@ -56,6 +56,70 @@ func (r *DockerInfrastructureProviderReconciler) SetupWithManager(mgr ctrl.Manag
5656 Complete (r )
5757}
5858
59+ // +kubebuilder:rbac:groups=infrastructure.crit.sh,resources=dockerinfrastructureproviders,verbs=get;list;watch
60+ // +kubebuilder:rbac:groups=infrastructure.crit.sh,resources=dockerinfrastructureproviders/status,verbs=create;update
61+ // +kubebuilder:rbac:groups=machine.crit.sh,resources=infrastructureproviders;infrastructureproviders/status,verbs=get;list;watch
62+ // +kubebuilder:rbac:groups=,resources=secrets,verbs=*
63+
64+ func (r * DockerInfrastructureProviderReconciler ) Reconcile (req ctrl.Request ) (_ ctrl.Result , reterr error ) {
65+ ctx := context .Background ()
66+ log := r .Log .WithValues ("dockerinfrastructureprovider" , req .NamespacedName )
67+
68+ ip := & v1alpha1.DockerInfrastructureProvider {}
69+ if err := r .Get (ctx , req .NamespacedName , ip ); err != nil {
70+ if apierrors .IsNotFound (err ) {
71+ return ctrl.Result {}, nil
72+ }
73+ return ctrl.Result {}, err
74+ }
75+
76+ ipOwner , err := util .GetOwnerInfrastructureProvider (ctx , r .Client , ip .ObjectMeta )
77+ if err != nil {
78+ return ctrl.Result {}, err
79+ }
80+ if ipOwner == nil {
81+ log .Info ("InfrastructureProvider Controller has not yet set OwnerRef" )
82+ return ctrl.Result {}, nil
83+ }
84+
85+ log = log .WithValues ("infrastructureprovider" , ipOwner .Name )
86+
87+ s := & corev1.Secret {
88+ ObjectMeta : metav1.ObjectMeta {
89+ Name : OpenAPISchemaSecretName ,
90+ Namespace : ip .Namespace ,
91+ },
92+ }
93+ if err := r .Get (ctx , client.ObjectKey {Name : s .Name , Namespace : s .Namespace }, s ); client .IgnoreNotFound (err ) != nil {
94+ return ctrl.Result {}, err
95+ }
96+
97+ ip .Status .Ready = ! s .GetCreationTimestamp ().Time .IsZero () // ready if secret already exists
98+ ip .Status .LastUpdated = metav1 .Now ()
99+ defer func () {
100+ if err := r .Status ().Update (ctx , ip ); err != nil {
101+ log .Error (err , "failed to update provider status" )
102+ }
103+ }()
104+
105+ b , err := json .Marshal (schema )
106+ if err != nil {
107+ return ctrl.Result {}, err
108+ }
109+
110+ if _ , err := controllerutil .CreateOrUpdate (ctx , r .Client , s , func () error {
111+ s .Data = map [string ][]byte {"schema" : b }
112+ return controllerutil .SetControllerReference (ip , s , r .Scheme )
113+ }); err != nil {
114+ return ctrl.Result {}, err
115+ }
116+
117+ ip .Status .Ready = true
118+ return ctrl.Result {}, nil
119+ }
120+
121+ const OpenAPISchemaSecretName = "config-schema"
122+
59123var schema = spec.Schema {
60124 SchemaProps : spec.SchemaProps {
61125 Type : spec.StringOrArray {"object" },
@@ -96,7 +160,7 @@ var schema = spec.Schema{
96160 SchemaProps : spec.SchemaProps {
97161 Type : spec.StringOrArray {"string" },
98162 Description : "container image to use" ,
99- Default : "criticalstack/cinder:v1.0.0-beta.10" ,
163+ Default : cinderapi . DefaultNodeImage ,
100164 },
101165 },
102166 "containerName" : {
@@ -121,49 +185,3 @@ var schema = spec.Schema{
121185 Required : []string {"apiVersion" , "kind" },
122186 },
123187}
124-
125- // +kubebuilder:rbac:groups=infrastructure.crit.sh,resources=dockerinfrastructureproviders,verbs=get;list;watch
126- // +kubebuilder:rbac:groups=infrastructure.crit.sh,resources=dockerinfrastructureproviders/status,verbs=create;update
127- // +kubebuilder:rbac:groups=,resources=secrets,verbs=*
128-
129- func (r * DockerInfrastructureProviderReconciler ) Reconcile (req ctrl.Request ) (_ ctrl.Result , reterr error ) {
130- ctx := context .Background ()
131- log := r .Log .WithValues ("dockerinfrastructureprovider" , req .NamespacedName )
132-
133- ip := & v1alpha1.DockerInfrastructureProvider {}
134- if err := r .Get (ctx , req .NamespacedName , ip ); err != nil {
135- if apierrors .IsNotFound (err ) {
136- return ctrl.Result {}, nil
137- }
138- return ctrl.Result {}, err
139- }
140-
141- var s corev1.Secret
142- s .SetName (OpenAPISchemaSecretName )
143- s .SetNamespace (ip .Namespace )
144- if err := r .Get (ctx , client.ObjectKey {Name : s .Name , Namespace : s .Namespace }, & s ); client .IgnoreNotFound (err ) != nil {
145- return ctrl.Result {}, err
146- }
147-
148- ip .Status .Ready = ! s .GetCreationTimestamp ().Time .IsZero () // ready if secret already exists
149- ip .Status .LastUpdated = metav1 .Now ()
150- defer func () {
151- if err := r .Status ().Update (ctx , ip ); err != nil {
152- log .Error (err , "failed to update provider status" )
153- }
154- }()
155- b , err := json .Marshal (schema )
156- if err != nil {
157- return ctrl.Result {}, err
158- }
159-
160- if _ , err := controllerutil .CreateOrUpdate (ctx , r .Client , & s , func () error {
161- s .Data = map [string ][]byte {"schema" : b }
162- return controllerutil .SetControllerReference (ip , & s , r .Scheme )
163- }); err != nil {
164- return ctrl.Result {}, err
165- }
166-
167- ip .Status .Ready = true
168- return ctrl.Result {}, nil
169- }
0 commit comments