Skip to content

Commit 49d87ef

Browse files
committed
fix panics in package-server
1 parent cc1b9ef commit 49d87ef

File tree

5 files changed

+63
-36
lines changed

5 files changed

+63
-36
lines changed

cmd/package-server/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ var (
3636
func init() {
3737
flags := cmd.Flags()
3838

39-
// flags.BoolVar(&options.InsecureKubeletTLS, "kubelet-insecure-tls", options.InsecureKubeletTLS, "Do not verify CA of serving certificates presented by Kubelets. For testing purposes only.")
4039
flags.DurationVar(&options.WakeupInterval, "interval", options.WakeupInterval, "Interval at which to re-sync CatalogSources")
4140
flags.StringVar(&options.GlobalNamespace, "global-namespace", options.GlobalNamespace, "Name of the namespace where the global CatalogSources are located")
4241
flags.StringSliceVar(&options.WatchedNamespaces, "watched-namespaces", options.WatchedNamespaces, "List of namespaces the package-server will watch watch for CatalogSources")

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ require (
7474
k8s.io/code-generator v0.0.0-20180904193909-8c97d6ab64da
7575
k8s.io/gengo v0.0.0-20181106084056-51747d6e00da // indirect
7676
k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92 // indirect
77-
k8s.io/kube-aggregator v0.0.0-20181121072050-af204e4cff09
77+
k8s.io/kube-aggregator v0.0.0-20180905000155-efa32eb095fe
7878
k8s.io/kube-openapi v0.0.0-20181031203759-72693cb1fadd
7979
k8s.io/kubernetes v1.11.6-beta.0.0.20181126160157-5933b9771b71
8080
)

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,6 @@ k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92 h1:PgoMI/L1Nu5Vmvgm+vGheLuxKST8h6
228228
k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
229229
k8s.io/kube-aggregator v0.0.0-20180905000155-efa32eb095fe h1:LM48rywzVEPRg+Os2oUL9/vsztPQGoxmiD3m5VySchw=
230230
k8s.io/kube-aggregator v0.0.0-20180905000155-efa32eb095fe/go.mod h1:8sbzT4QQKDEmSCIbfqjV0sd97GpUT7A4W626sBiYJmU=
231-
k8s.io/kube-aggregator v0.0.0-20181121072050-af204e4cff09 h1:v5wOckd8yeVJcWcnE0xLdW60/Qrd17gXxW24O3aiNxg=
232-
k8s.io/kube-aggregator v0.0.0-20181121072050-af204e4cff09/go.mod h1:8sbzT4QQKDEmSCIbfqjV0sd97GpUT7A4W626sBiYJmU=
233231
k8s.io/kube-openapi v0.0.0-20181031203759-72693cb1fadd h1:ggv/Vfza0i5xuhUZyYyxcc25AmQvHY8Zi1C2m8WgBvA=
234232
k8s.io/kube-openapi v0.0.0-20181031203759-72693cb1fadd/go.mod h1:BXM9ceUBTj2QnfH2MK1odQs778ajze1RxcmP6S8RVVc=
235233
k8s.io/kubernetes v1.11.6-beta.0.0.20181126160157-5933b9771b71 h1:ZiDzUVY+KNDO1sbcG0hHZokQsNIhjCCCsy06Z4Ck4JA=

pkg/package-server/provider/inmem.go

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,22 @@ func parsePackageManifestsFromConfigMap(cm *corev1.ConfigMap, catsrc *operatorsv
9191
logger.Debug("ConfigMap contains CSVs")
9292
csvListJSON, err := yaml.YAMLToJSON([]byte(csvListYaml))
9393
if err != nil {
94-
logrus.Debugf("Load ConfigMap -- ERROR %s : error=%s", cmName, err)
94+
logger.Debugf("Load ConfigMap -- ERROR %s : error=%s", cmName, err)
9595
return nil, fmt.Errorf("error loading CSV list yaml from ConfigMap %s: %s", cmName, err)
9696
}
9797

9898
var parsedCSVList []operatorsv1alpha1.ClusterServiceVersion
9999
err = json.Unmarshal([]byte(csvListJSON), &parsedCSVList)
100100
if err != nil {
101-
logrus.Debugf("Load ConfigMap -- ERROR %s : error=%s", cmName, err)
101+
logger.Debugf("Load ConfigMap -- ERROR %s : error=%s", cmName, err)
102102
return nil, fmt.Errorf("error parsing CSV list (json) from ConfigMap %s: %s", cmName, err)
103103
}
104104

105105
for _, csv := range parsedCSVList {
106106
found = true
107107

108108
// TODO: add check for invalid CSV definitions
109-
logrus.Debugf("found csv %s", csv.GetName())
109+
logger.Debugf("found csv %s", csv.GetName())
110110
csvs[csv.GetName()] = csv
111111
}
112112
}
@@ -175,7 +175,7 @@ func parsePackageManifestsFromConfigMap(cm *corev1.ConfigMap, catsrc *operatorsv
175175
manifest.ObjectMeta.Labels[k] = v
176176
}
177177

178-
logrus.Debugf("retrieved packagemanifest %s", manifest.GetName())
178+
logger.Debugf("retrieved packagemanifest %s", manifest.GetName())
179179
manifests = append(manifests, manifest)
180180
}
181181
}
@@ -196,6 +196,12 @@ func (m *InMemoryProvider) syncCatalogSource(obj interface{}) error {
196196
return fmt.Errorf("casting catalog source failed")
197197
}
198198

199+
logger := logrus.WithFields(logrus.Fields{
200+
"Action": "Sync CatalogSource",
201+
"name": catsrc.GetName(),
202+
"namespace": catsrc.GetNamespace(),
203+
})
204+
199205
var manifests []packagev1alpha1.PackageManifest
200206

201207
// handle by sourceType
@@ -217,7 +223,7 @@ func (m *InMemoryProvider) syncCatalogSource(obj interface{}) error {
217223
return fmt.Errorf("catalog source %s in namespace %s source type %s not recognized", catsrc.GetName(), catsrc.GetNamespace(), catsrc.Spec.SourceType)
218224
}
219225

220-
// update manifests
226+
logger.Debug("updating in-memory PackageManifests")
221227
m.mu.Lock()
222228
defer m.mu.Unlock()
223229
for _, manifest := range manifests {
@@ -228,18 +234,18 @@ func (m *InMemoryProvider) syncCatalogSource(obj interface{}) error {
228234
}
229235

230236
if pm, ok := m.manifests[key]; ok {
231-
// use existing CreationTimestamp
237+
logger.Debugf("package %s already exists", key.packageName)
232238
manifest.CreationTimestamp = pm.ObjectMeta.CreationTimestamp
233239
} else {
234-
// set CreationTimestamp if first time seeing the PackageManifest
240+
logger.Debugf("new package %s found", key.packageName)
235241
manifest.CreationTimestamp = metav1.NewTime(time.Now())
236242
for _, add := range m.add {
237243
if add.namespace == manifest.Status.CatalogSourceNamespace || add.namespace == metav1.NamespaceAll || manifest.Status.CatalogSourceNamespace == m.globalNamespace {
244+
logger.Debugf("sending new package %s to watcher for namespace %s", key.packageName, add.namespace)
238245
add.ch <- manifest
239246
}
240247
}
241248
}
242-
243249
m.manifests[key] = manifest
244250
}
245251

@@ -276,45 +282,48 @@ func (m *InMemoryProvider) List(namespace string) (*packagev1alpha1.PackageManif
276282
matching = append(matching, pm)
277283
}
278284
}
279-
280285
manifestList.Items = matching
281286
}
282-
283287
return manifestList, nil
284288
}
285289

286290
func (m *InMemoryProvider) Subscribe(namespace string, stopCh <-chan struct{}) (PackageChan, PackageChan, PackageChan, error) {
291+
logger := logrus.WithFields(logrus.Fields{
292+
"Action": "PackageManifest Subscribe",
293+
"namespace": namespace,
294+
})
295+
287296
m.mu.Lock()
288297
defer m.mu.Unlock()
289298

290-
add := eventChan{namespace, make(chan packagev1alpha1.PackageManifest)}
291-
modify := eventChan{namespace, make(chan packagev1alpha1.PackageManifest)}
292-
delete := eventChan{namespace, make(chan packagev1alpha1.PackageManifest)}
293-
addIndex := len(m.add)
294-
modifyIndex := len(m.modify)
295-
deleteIndex := len(m.delete)
296-
m.add = append(m.add, add)
297-
m.modify = append(m.modify, modify)
298-
m.delete = append(m.delete, delete)
299+
addEvent := eventChan{namespace, make(chan packagev1alpha1.PackageManifest)}
300+
modifyEvent := eventChan{namespace, make(chan packagev1alpha1.PackageManifest)}
301+
deleteEvent := eventChan{namespace, make(chan packagev1alpha1.PackageManifest)}
302+
m.add = append(m.add, addEvent)
303+
m.modify = append(m.modify, modifyEvent)
304+
m.delete = append(m.delete, deleteEvent)
305+
306+
removeChan := func(target chan packagev1alpha1.PackageManifest, all []eventChan) []eventChan {
307+
for i, event := range all {
308+
if event.ch == target {
309+
logger.Debugf("closing channel")
310+
close(event.ch)
311+
return append(all[:i], all[i+1:]...)
312+
}
313+
}
314+
return all
315+
}
299316

300317
go func() {
301318
<-stopCh
302319
m.mu.Lock()
303320
defer m.mu.Unlock()
304-
for _, add := range m.add {
305-
m.add = append(m.add[:addIndex], m.add[:addIndex+1]...)
306-
close(add.ch)
307-
}
308-
for _, modify := range m.modify {
309-
m.modify = append(m.modify[:modifyIndex], m.modify[:modifyIndex+1]...)
310-
close(modify.ch)
311-
}
312-
for _, delete := range m.delete {
313-
m.delete = append(m.delete[:deleteIndex], m.delete[:deleteIndex+1]...)
314-
close(delete.ch)
315-
}
321+
322+
m.add = removeChan(addEvent.ch, m.add)
323+
m.modify = removeChan(modifyEvent.ch, m.modify)
324+
m.delete = removeChan(deleteEvent.ch, m.delete)
316325
return
317326
}()
318327

319-
return add.ch, modify.ch, delete.ch, nil
328+
return addEvent.ch, modifyEvent.ch, deleteEvent.ch, nil
320329
}

test/e2e/packagemanifest_e2e_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ func TestPackageManifestLoading(t *testing.T) {
8484
}
8585

8686
watcher, err := pmc.PackagemanifestV1alpha1().PackageManifests(testNamespace).Watch(metav1.ListOptions{})
87+
defer watcher.Stop()
8788
require.NoError(t, err)
89+
receivedPackage := make(chan bool)
8890
go func() {
8991
event := <-watcher.ResultChan()
9092
pkg := event.Object.(*packagev1alpha1.PackageManifest)
@@ -93,6 +95,7 @@ func TestPackageManifestLoading(t *testing.T) {
9395
require.NotNil(t, pkg)
9496
require.Equal(t, packageName, pkg.GetName())
9597
require.Equal(t, expectedStatus, pkg.Status)
98+
receivedPackage <- true
9699
return
97100
}()
98101

@@ -102,8 +105,26 @@ func TestPackageManifestLoading(t *testing.T) {
102105

103106
pm, err := fetchPackageManifest(t, pmc, testNamespace, packageName, packageManifestHasStatus)
104107

108+
require.True(t, <-receivedPackage)
105109
require.NoError(t, err, "error getting package manifest")
106110
require.NotNil(t, pm)
107111
require.Equal(t, packageName, pm.GetName())
108112
require.Equal(t, expectedStatus, pm.Status)
109113
}
114+
115+
func TestPackageManifestMultipleWatches(t *testing.T) {
116+
pmc := newPMClient(t)
117+
118+
watcherA, _ := pmc.PackagemanifestV1alpha1().PackageManifests(testNamespace).Watch(metav1.ListOptions{})
119+
watcherB, _ := pmc.PackagemanifestV1alpha1().PackageManifests(testNamespace).Watch(metav1.ListOptions{})
120+
watcherC, _ := pmc.PackagemanifestV1alpha1().PackageManifests(testNamespace).Watch(metav1.ListOptions{})
121+
122+
defer watcherB.Stop()
123+
defer watcherC.Stop()
124+
watcherA.Stop()
125+
126+
list, err := pmc.PackagemanifestV1alpha1().PackageManifests(testNamespace).List(metav1.ListOptions{})
127+
128+
require.NoError(t, err)
129+
require.NotEqual(t, 0, len(list.Items))
130+
}

0 commit comments

Comments
 (0)