Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OCPBUGS-19695: gather APIServer.config.openshift.io resource (#825)
* Add draft gatherer and sample file * Fix signature and Add unit tests * Update documentation * Rename files to follow name convention * Fix naming convention on doc and function names
- Loading branch information
Showing
5 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"metadata": { | ||
"name": "cluster", | ||
"uid": "fa053692-3123-4df1-8e3d-1875d820035f", | ||
"resourceVersion": "894", | ||
"generation": 1, | ||
"creationTimestamp": "2023-09-21T13:47:51Z", | ||
"annotations": { | ||
"include.release.openshift.io/ibm-cloud-managed": "true", | ||
"include.release.openshift.io/self-managed-high-availability": "true", | ||
"include.release.openshift.io/single-node-developer": "true", | ||
"oauth-apiserver.openshift.io/secure-token-storage": "true", | ||
"release.openshift.io/create-only": "true" | ||
}, | ||
"ownerReferences": [ | ||
{ | ||
"apiVersion": "config.openshift.io/v1", | ||
"kind": "ClusterVersion", | ||
"name": "version", | ||
"uid": "02631234-a2cb-4cd1-b444-032d8c0456ae" | ||
} | ||
] | ||
}, | ||
"spec": { | ||
"servingCerts": {}, | ||
"clientCA": { | ||
"name": "" | ||
}, | ||
"encryption": {}, | ||
"audit": { | ||
"profile": "Default" | ||
} | ||
}, | ||
"status": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package clusterconfig | ||
|
||
import ( | ||
"context" | ||
|
||
configv1client "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1" | ||
"github.com/openshift/insights-operator/pkg/record" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// GatherClusterAPIServer Collects APIServer.config.openshift.io resource | ||
// | ||
// ### API Reference | ||
// https://github.com/openshift/api/blob/master/config/v1/types_apiserver.go | ||
// | ||
// ### Sample data | ||
// - docs/insights-archive-sample/config/apiserver.json | ||
// | ||
// ### Location in archive | ||
// - `config/apiserver.json` | ||
// | ||
// ### Config ID | ||
// `cluster_apiserver` | ||
// | ||
// ### Released version | ||
// - 4.15 | ||
// | ||
// ### Backported versions | ||
// - TBD | ||
// | ||
// ### Changes | ||
// None | ||
func (g *Gatherer) GatherClusterAPIServer(ctx context.Context) ([]record.Record, []error) { | ||
configClient, err := configv1client.NewForConfig(g.gatherKubeConfig) | ||
if err != nil { | ||
return nil, []error{err} | ||
} | ||
|
||
return clusterAPIServer{}.gather(ctx, configClient.APIServers()) | ||
} | ||
|
||
type clusterAPIServer struct{} | ||
|
||
func (cas clusterAPIServer) gather(ctx context.Context, apiservers configv1client.APIServerInterface) ([]record.Record, []error) { | ||
const APIServerName = "cluster" | ||
const Filename = "config/apiserver" | ||
|
||
server, err := apiservers.Get(ctx, APIServerName, v1.GetOptions{}) | ||
if err != nil { | ||
if errors.IsNotFound(err) { | ||
return nil, nil | ||
} | ||
return nil, []error{err} | ||
} | ||
|
||
return []record.Record{{Name: Filename, Item: record.ResourceMarshaller{Resource: server}}}, nil | ||
} |
56 changes: 56 additions & 0 deletions
56
pkg/gatherers/clusterconfig/gather_cluster_apiserver_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package clusterconfig | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
v1 "github.com/openshift/api/config/v1" | ||
configfake "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1/fake" | ||
"github.com/stretchr/testify/assert" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
func Test_GatherClusterAPIServer(t *testing.T) { | ||
// Unit Tests | ||
testCases := []struct { | ||
name string | ||
apiserver *v1.APIServer | ||
itemCount int | ||
errCount int | ||
}{ | ||
{ | ||
name: "No APIServer found returns no error", | ||
apiserver: &v1.APIServer{ObjectMeta: metav1.ObjectMeta{Name: "mock"}}, | ||
itemCount: 0, | ||
errCount: 0, | ||
}, | ||
{ | ||
name: "cluster APIServer is properly recorded", | ||
apiserver: &v1.APIServer{ObjectMeta: metav1.ObjectMeta{Name: "cluster"}}, | ||
itemCount: 1, | ||
errCount: 0, | ||
}, | ||
} | ||
|
||
for i := range testCases { | ||
tc := testCases[i] | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
// Given | ||
clientset := fake.NewSimpleClientset() | ||
configv1 := configfake.FakeConfigV1{Fake: &clientset.Fake} | ||
configv1.APIServers().Create(context.Background(), tc.apiserver, metav1.CreateOptions{}) // nolint: errcheck | ||
|
||
// When | ||
records, err := clusterAPIServer{}.gather(context.Background(), configv1.APIServers()) | ||
|
||
// Assert | ||
assert.Len(t, err, tc.errCount) | ||
assert.Len(t, records, tc.itemCount) | ||
if tc.itemCount > 0 { | ||
assert.Equal(t, "config/apiserver", records[0].Name) | ||
} | ||
}) | ||
} | ||
} |