|
1 | 1 | package clientcmd
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "encoding/json" |
5 | 4 | "fmt"
|
6 | 5 |
|
7 |
| - "github.com/blang/semver" |
| 6 | + "k8s.io/apimachinery/pkg/api/errors" |
| 7 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 8 | + "k8s.io/client-go/discovery" |
8 | 9 |
|
9 |
| - "github.com/openshift/origin/pkg/client" |
10 |
| - "github.com/openshift/origin/pkg/version" |
| 10 | + "github.com/openshift/origin/pkg/authorization/apis/authorization" |
11 | 11 | )
|
12 | 12 |
|
13 |
| -// Gate returns an error if the server is below minServerVersion or above/equal maxServerVersion. |
14 |
| -// To test only for min or only max version, set the other string to the empty value. |
15 |
| -func Gate(ocClient *client.Client, minServerVersion, maxServerVersion string) error { |
16 |
| - if len(minServerVersion) == 0 && len(maxServerVersion) == 0 { |
17 |
| - return fmt.Errorf("No version info passed to gate command") |
18 |
| - } |
| 13 | +// LegacyPolicyResourceGate returns err if the server does not support the set of legacy policy objects (< 3.7) |
| 14 | +func LegacyPolicyResourceGate(client discovery.DiscoveryInterface) error { |
| 15 | + // The server must support the 4 legacy policy objects in either of the GV schemes. |
| 16 | + _, all, err := DiscoverGroupVersionResources(client, |
| 17 | + schema.GroupVersionResource{ |
| 18 | + Group: authorization.LegacySchemeGroupVersion.Group, |
| 19 | + Version: authorization.LegacySchemeGroupVersion.Version, |
| 20 | + Resource: "clusterpolicies", |
| 21 | + }, |
| 22 | + schema.GroupVersionResource{ |
| 23 | + Group: authorization.LegacySchemeGroupVersion.Group, |
| 24 | + Version: authorization.LegacySchemeGroupVersion.Version, |
| 25 | + Resource: "clusterpolicybindings", |
| 26 | + }, |
| 27 | + schema.GroupVersionResource{ |
| 28 | + Group: authorization.LegacySchemeGroupVersion.Group, |
| 29 | + Version: authorization.LegacySchemeGroupVersion.Version, |
| 30 | + Resource: "policies", |
| 31 | + }, |
| 32 | + schema.GroupVersionResource{ |
| 33 | + Group: authorization.LegacySchemeGroupVersion.Group, |
| 34 | + Version: authorization.LegacySchemeGroupVersion.Version, |
| 35 | + Resource: "policybindings", |
| 36 | + }, |
| 37 | + ) |
19 | 38 |
|
20 |
| - ocVersionBody, err := ocClient.Get().AbsPath("/version/openshift").Do().Raw() |
21 | 39 | if err != nil {
|
22 | 40 | return err
|
23 | 41 | }
|
24 |
| - ocServerInfo := &version.Info{} |
25 |
| - if err := json.Unmarshal(ocVersionBody, ocServerInfo); err != nil { |
26 |
| - return err |
| 42 | + if all { |
| 43 | + return nil |
27 | 44 | }
|
28 |
| - ocVersion := ocServerInfo.String() |
29 |
| - // skip first chracter as Openshift returns a 'v' preceding the actual |
30 |
| - // version string which semver does not grok |
31 |
| - semVersion, err := semver.Parse(ocVersion[1:]) |
| 45 | + _, all, err = DiscoverGroupVersionResources(client, |
| 46 | + schema.GroupVersionResource{ |
| 47 | + Group: authorization.SchemeGroupVersion.Group, |
| 48 | + Version: authorization.SchemeGroupVersion.Version, |
| 49 | + Resource: "clusterpolicies", |
| 50 | + }, |
| 51 | + schema.GroupVersionResource{ |
| 52 | + Group: authorization.SchemeGroupVersion.Group, |
| 53 | + Version: authorization.SchemeGroupVersion.Version, |
| 54 | + Resource: "clusterpolicybindings", |
| 55 | + }, |
| 56 | + schema.GroupVersionResource{ |
| 57 | + Group: authorization.SchemeGroupVersion.Group, |
| 58 | + Version: authorization.SchemeGroupVersion.Version, |
| 59 | + Resource: "policies", |
| 60 | + }, |
| 61 | + schema.GroupVersionResource{ |
| 62 | + Group: authorization.SchemeGroupVersion.Group, |
| 63 | + Version: authorization.SchemeGroupVersion.Version, |
| 64 | + Resource: "policybindings", |
| 65 | + }, |
| 66 | + ) |
| 67 | + |
32 | 68 | if err != nil {
|
33 |
| - return fmt.Errorf("Failed to parse server version %s: %v", ocVersion, err) |
| 69 | + return err |
| 70 | + } |
| 71 | + if all { |
| 72 | + return nil |
34 | 73 | }
|
35 |
| - // ignore pre-release version info |
36 |
| - semVersion.Pre = nil |
37 | 74 |
|
38 |
| - if len(minServerVersion) > 0 { |
39 |
| - min, err := semver.Parse(minServerVersion) |
40 |
| - if err != nil { |
41 |
| - return fmt.Errorf("Failed to parse min gate version %s: %v", minServerVersion, err) |
42 |
| - } |
43 |
| - // ignore pre-release version info |
44 |
| - min.Pre = nil |
45 |
| - if semVersion.LT(min) { |
46 |
| - return fmt.Errorf("This command works only with server versions > %s, found %s", minServerVersion, ocVersion) |
47 |
| - } |
| 75 | + return fmt.Errorf("the server does not support legacy policy resources") |
| 76 | +} |
| 77 | + |
| 78 | +// DiscoverGroupVersionResources performs a server resource discovery for each filterGVR, returning a slice of |
| 79 | +// GVRs for the matching Resources, and a bool for "all" indicating that each item in filterGVR was found. |
| 80 | +func DiscoverGroupVersionResources(client discovery.ServerResourcesInterface, filterGVR ...schema.GroupVersionResource) ([]schema.GroupVersionResource, bool, error) { |
| 81 | + if len(filterGVR) == 0 { |
| 82 | + return nil, false, fmt.Errorf("at least one GroupVersionResource must be provided") |
48 | 83 | }
|
49 | 84 |
|
50 |
| - if len(maxServerVersion) > 0 { |
51 |
| - max, err := semver.Parse(maxServerVersion) |
| 85 | + all := true |
| 86 | + |
| 87 | + // var cache map[schema.GroupVersion][]string TODO: possibly needed if not cached |
| 88 | + |
| 89 | + ret := []schema.GroupVersionResource{} |
| 90 | + for i := range filterGVR { |
| 91 | + // Discover the list of resources for this GVR |
| 92 | + gv := filterGVR[i].GroupVersion() |
| 93 | + serverResources, err := client.ServerResourcesForGroupVersion(gv.String()) |
| 94 | + if err != nil && errors.IsNotFound(err) { |
| 95 | + all = false |
| 96 | + continue |
| 97 | + } |
52 | 98 | if err != nil {
|
53 |
| - return fmt.Errorf("Failed to parse max gate version %s: %v", maxServerVersion, err) |
| 99 | + return nil, false, err |
54 | 100 | }
|
55 |
| - // ignore pre-release version info |
56 |
| - max.Pre = nil |
57 |
| - if semVersion.GTE(max) { |
58 |
| - return fmt.Errorf("This command works only with server versions < %s, found %s", maxServerVersion, ocVersion) |
| 101 | + |
| 102 | + seen := false |
| 103 | + // Add matching resources to the return slice |
| 104 | + for _, resource := range serverResources.APIResources { |
| 105 | + if filterGVR[i].Resource == resource.Name { |
| 106 | + seen = true |
| 107 | + ret = append(ret, filterGVR[i]) |
| 108 | + break |
| 109 | + } |
59 | 110 | }
|
| 111 | + all = all && seen |
60 | 112 | }
|
61 | 113 |
|
62 |
| - // OK this is within min/max all good! |
63 |
| - return nil |
| 114 | + return ret, all, nil |
64 | 115 | }
|
0 commit comments