9
9
k8serror "k8s.io/apimachinery/pkg/api/errors"
10
10
"k8s.io/apimachinery/pkg/api/resource"
11
11
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12
+ utilerrors "k8s.io/apimachinery/pkg/util/errors"
12
13
"k8s.io/apimachinery/pkg/util/intstr"
13
14
k8scontrollerclient "sigs.k8s.io/controller-runtime/pkg/client"
14
15
)
@@ -22,6 +23,7 @@ const (
22
23
23
24
type MagicCatalog interface {
24
25
DeployCatalog (ctx context.Context ) error
26
+ UpdateCatalog (ctx context.Context , provider FileBasedCatalogProvider ) error
25
27
UndeployCatalog (ctx context.Context ) []error
26
28
}
27
29
@@ -50,74 +52,119 @@ func NewMagicCatalog(kubeClient k8scontrollerclient.Client, namespace string, ca
50
52
}
51
53
52
54
func (c * magicCatalog ) DeployCatalog (ctx context.Context ) error {
53
-
54
55
catalogSource := c .makeCatalogSource ()
55
56
resourcesInOrderOfDeployment := []k8scontrollerclient.Object {
56
57
c .makeConfigMap (),
57
58
c .makeCatalogSourcePod (),
58
59
c .makeCatalogService (),
59
60
catalogSource ,
60
61
}
62
+ if err := c .deployCatalog (ctx , resourcesInOrderOfDeployment ); err != nil {
63
+ return err
64
+ }
65
+ if err := catalogSourceIsReady (ctx , c .kubeClient , catalogSource ); err != nil {
66
+ return c .cleanUpAfter (ctx , err )
67
+ }
68
+
69
+ return nil
70
+ }
71
+
72
+ func catalogSourceIsReady (ctx context.Context , c k8scontrollerclient.Client , cs * operatorsv1alpha1.CatalogSource ) error {
73
+ // wait for catalog source to become ready
74
+ return waitFor (func () (bool , error ) {
75
+ err := c .Get (ctx , k8scontrollerclient.ObjectKey {
76
+ Name : cs .GetName (),
77
+ Namespace : cs .GetNamespace (),
78
+ }, cs )
79
+ if err != nil || cs .Status .GRPCConnectionState == nil {
80
+ return false , err
81
+ }
82
+ state := cs .Status .GRPCConnectionState .LastObservedState
83
+ if state != catalogReadyState {
84
+ return false , nil
85
+ }
86
+ return true , nil
87
+ })
88
+ }
61
89
62
- for _ , res := range resourcesInOrderOfDeployment {
90
+ func (c * magicCatalog ) deployCatalog (ctx context.Context , resources []k8scontrollerclient.Object ) error {
91
+ for _ , res := range resources {
63
92
err := c .kubeClient .Create (ctx , res )
64
93
if err != nil {
65
94
return c .cleanUpAfter (ctx , err )
66
95
}
67
96
}
97
+ return nil
98
+ }
68
99
69
- // wait for catalog source to become ready
100
+ func (c * magicCatalog ) UpdateCatalog (ctx context.Context , provider FileBasedCatalogProvider ) error {
101
+ resourcesInOrderOfDeletion := []k8scontrollerclient.Object {
102
+ c .makeCatalogSourcePod (),
103
+ c .makeConfigMap (),
104
+ }
105
+ errors := c .undeployCatalog (ctx , resourcesInOrderOfDeletion )
106
+ if len (errors ) != 0 {
107
+ return utilerrors .NewAggregate (errors )
108
+ }
109
+
110
+ // TODO(tflannag): Create a pod watcher struct and setup an underlying watch
111
+ // and block until ctx.Done()?
70
112
err := waitFor (func () (bool , error ) {
113
+ pod := & corev1.Pod {}
71
114
err := c .kubeClient .Get (ctx , k8scontrollerclient.ObjectKey {
72
- Name : catalogSource .GetName (),
73
- Namespace : catalogSource .GetNamespace (),
74
- }, catalogSource )
75
-
76
- if err != nil || catalogSource .Status .GRPCConnectionState == nil {
77
- return false , err
78
- }
79
-
80
- state := catalogSource .Status .GRPCConnectionState .LastObservedState
81
-
82
- if state != catalogReadyState {
83
- return false , nil
84
- } else {
115
+ Name : c .podName ,
116
+ Namespace : c .namespace ,
117
+ }, pod )
118
+ if k8serror .IsNotFound (err ) {
85
119
return true , nil
86
120
}
121
+ return false , err
87
122
})
88
-
89
123
if err != nil {
124
+ return fmt .Errorf ("failed to successfully update the catalog deployment: %v" , err )
125
+ }
126
+
127
+ c .fileBasedCatalog = provider
128
+ resourcesInOrderOfCreation := []k8scontrollerclient.Object {
129
+ c .makeConfigMap (),
130
+ c .makeCatalogSourcePod (),
131
+ }
132
+ if err := c .deployCatalog (ctx , resourcesInOrderOfCreation ); err != nil {
133
+ return err
134
+ }
135
+ if err := catalogSourceIsReady (ctx , c .kubeClient , c .makeCatalogSource ()); err != nil {
90
136
return c .cleanUpAfter (ctx , err )
91
137
}
92
138
93
139
return nil
94
140
}
95
141
96
142
func (c * magicCatalog ) UndeployCatalog (ctx context.Context ) []error {
97
- var errs []error = nil
98
-
99
143
resourcesInOrderOfDeletion := []k8scontrollerclient.Object {
100
144
c .makeCatalogSource (),
101
145
c .makeCatalogService (),
102
146
c .makeCatalogSourcePod (),
103
147
c .makeConfigMap (),
104
148
}
149
+ return c .undeployCatalog (ctx , resourcesInOrderOfDeletion )
150
+ }
105
151
152
+ func (c * magicCatalog ) undeployCatalog (ctx context.Context , resources []k8scontrollerclient.Object ) []error {
153
+ var errors []error
106
154
// try to delete all resourcesInOrderOfDeletion even if errors are
107
155
// encountered through deletion.
108
- for _ , res := range resourcesInOrderOfDeletion {
156
+ for _ , res := range resources {
109
157
err := c .kubeClient .Delete (ctx , res )
110
158
111
159
// ignore not found errors
112
160
if err != nil && ! k8serror .IsNotFound (err ) {
113
- if errs == nil {
114
- errs = make ([]error , 0 )
161
+ if errors == nil {
162
+ errors = make ([]error , 0 )
115
163
}
116
- errs = append (errs , err )
164
+ errors = append (errors , err )
117
165
}
118
166
}
119
-
120
- return errs
167
+ return errors
121
168
}
122
169
123
170
func (c * magicCatalog ) cleanUpAfter (ctx context.Context , err error ) error {
0 commit comments