Skip to content

Commit

Permalink
OCPBUGS-19695: gather APIServer.config.openshift.io resource (#825)
Browse files Browse the repository at this point in the history
* 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
ncaak committed Oct 9, 2023
1 parent 8ca514d commit 4969600
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docs/gathered-data.md
Expand Up @@ -163,6 +163,32 @@ None
None


## ClusterAPIServer

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](./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


## ClusterAuthentication

Collects the cluster `Authentication` with cluster name.
Expand Down
35 changes: 35 additions & 0 deletions docs/insights-archive-sample/config/apiserver.json
@@ -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": {}
}
1 change: 1 addition & 0 deletions pkg/gatherers/clusterconfig/clusterconfig_gatherer.go
Expand Up @@ -31,6 +31,7 @@ var gatheringFunctions = map[string]gathererFuncPtr{
"authentication": (*Gatherer).GatherClusterAuthentication,
"certificate_signing_requests": (*Gatherer).GatherCertificateSigningRequests,
"ceph_cluster": (*Gatherer).GatherCephCluster,
"cluster_apiserver": (*Gatherer).GatherClusterAPIServer,
"config_maps": (*Gatherer).GatherConfigMaps,
"container_images": (*Gatherer).GatherContainerImages,
"container_runtime_configs": (*Gatherer).GatherContainerRuntimeConfig,
Expand Down
58 changes: 58 additions & 0 deletions pkg/gatherers/clusterconfig/gather_cluster_apiserver.go
@@ -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 pkg/gatherers/clusterconfig/gather_cluster_apiserver_test.go
@@ -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)
}
})
}
}

0 comments on commit 4969600

Please sign in to comment.