forked from kubernetes-sigs/kube-storage-version-migrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexer.go
72 lines (59 loc) · 2.7 KB
/
indexer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"fmt"
"reflect"
migration_v1alpha1 "github.com/kubernetes-sigs/kube-storage-version-migrator/pkg/apis/migration/v1alpha1"
migrationclient "github.com/kubernetes-sigs/kube-storage-version-migrator/pkg/clients/clientset"
migrationinformer "github.com/kubernetes-sigs/kube-storage-version-migrator/pkg/clients/informer/migration/v1alpha1"
"k8s.io/client-go/tools/cache"
)
const (
StatusIndex = "Status"
StatusRunning = "Running"
StatusPending = "Pending"
StatusCompleted = "Completed"
ResourceIndex = "Resource"
)
// migrationStatusIndexFunc categorizes StorageVersionMigrations based on their conditions.
func migrationStatusIndexFunc(obj interface{}) ([]string, error) {
m, ok := obj.(*migration_v1alpha1.StorageVersionMigration)
if !ok {
return []string{}, fmt.Errorf("expected StroageVersionMigration, got %#v", reflect.TypeOf(obj))
}
if HasCondition(m, migration_v1alpha1.MigrationSucceeded) || HasCondition(m, migration_v1alpha1.MigrationFailed) {
return []string{StatusCompleted}, nil
}
if HasCondition(m, migration_v1alpha1.MigrationRunning) {
return []string{StatusRunning}, nil
}
return []string{StatusPending}, nil
}
func NewStatusIndexedInformer(c migrationclient.Interface) cache.SharedIndexInformer {
return migrationinformer.NewStorageVersionMigrationInformer(c, 0, cache.Indexers{StatusIndex: migrationStatusIndexFunc})
}
func ToIndex(r migration_v1alpha1.GroupVersionResource) string {
return r.Resource + "." + r.Group
}
// migrationResourceIndexFunc categorizes StorageVersionMigrations based on the <.spec.resource.resource>.<.spec.resource.group>.
func migrationResourceIndexFunc(obj interface{}) ([]string, error) {
m, ok := obj.(*migration_v1alpha1.StorageVersionMigration)
if !ok {
return []string{}, fmt.Errorf("expected StroageVersionMigration, got %#v", reflect.TypeOf(obj))
}
return []string{ToIndex(m.Spec.Resource)}, nil
}
func NewStatusAndResourceIndexedInformer(c migrationclient.Interface) cache.SharedIndexInformer {
return migrationinformer.NewStorageVersionMigrationInformer(c, 0, cache.Indexers{StatusIndex: migrationStatusIndexFunc, ResourceIndex: migrationResourceIndexFunc})
}