Skip to content

Commit 228bc4b

Browse files
committed
diagnostics: enable per-diagnostic parameters
Adds the ability to specify parameters for individual diagnostics on the command line (without proliferating flags). Addresses #14640
1 parent 2e4c2ab commit 228bc4b

36 files changed

+3037
-372
lines changed

contrib/completions/bash/oc

+1,209-14
Large diffs are not rendered by default.

contrib/completions/zsh/oc

+1,209-14
Large diffs are not rendered by default.

pkg/oc/admin/diagnostics/client.go

+23-23
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@ import (
66
"k8s.io/apimachinery/pkg/util/sets"
77
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
88

9+
"github.com/openshift/origin/pkg/cmd/util/variable"
910
clientdiags "github.com/openshift/origin/pkg/oc/admin/diagnostics/diagnostics/client"
1011
networkdiags "github.com/openshift/origin/pkg/oc/admin/diagnostics/diagnostics/network"
1112
"github.com/openshift/origin/pkg/oc/admin/diagnostics/diagnostics/types"
1213
)
1314

14-
var (
15-
// availableClientDiagnostics contains the names of client diagnostics that can be executed
16-
// during a single run of diagnostics. Add more diagnostics to the list as they are defined.
17-
availableClientDiagnostics = sets.NewString(clientdiags.ConfigContextsName, clientdiags.DiagnosticPodName, networkdiags.NetworkDiagnosticName)
18-
)
15+
// availableClientDiagnostics returns definitions of client diagnostics that can be executed
16+
// during a single run of diagnostics. Add more diagnostics to the list as they are defined.
17+
func availableClientDiagnostics() types.DiagnosticList {
18+
return types.DiagnosticList{clientdiags.ConfigContext{}, &clientdiags.DiagnosticPod{}, &networkdiags.NetworkDiagnostic{}}
19+
}
20+
21+
func clientParameterizedDiagnostics() types.ParameterizedDiagnosticMap {
22+
return types.NewParameterizedDiagnosticMap(&networkdiags.NetworkDiagnostic{})
23+
}
1924

2025
// buildClientDiagnostics builds client Diagnostic objects based on the rawConfig passed in.
2126
// Returns the Diagnostics built, "ok" bool for whether to proceed or abort, and an error if any was encountered during the building of diagnostics.) {
22-
func (o DiagnosticsOptions) buildClientDiagnostics(rawConfig *clientcmdapi.Config) ([]types.Diagnostic, bool, error) {
23-
available := availableClientDiagnostics
27+
func (o DiagnosticsConfig) buildClientDiagnostics(rawConfig *clientcmdapi.Config) ([]types.Diagnostic, bool, error) {
28+
available := availableClientDiagnostics().Names()
2429

2530
networkClient, err := o.Factory.OpenshiftInternalNetworkClient()
2631
kubeClient, clientErr := o.Factory.ClientSet()
@@ -30,7 +35,7 @@ func (o DiagnosticsOptions) buildClientDiagnostics(rawConfig *clientcmdapi.Confi
3035
}
3136

3237
diagnostics := []types.Diagnostic{}
33-
requestedDiagnostics := available.Intersection(sets.NewString(o.RequestedDiagnostics...)).List()
38+
requestedDiagnostics := available.Intersection(sets.NewString(o.RequestedDiagnostics.List()...)).List()
3439
for _, diagnosticName := range requestedDiagnostics {
3540
switch diagnosticName {
3641
case clientdiags.ConfigContextsName:
@@ -52,23 +57,18 @@ func (o DiagnosticsOptions) buildClientDiagnostics(rawConfig *clientcmdapi.Confi
5257
Level: o.LogOptions.Level,
5358
Factory: o.Factory,
5459
PreventModification: o.PreventModification,
55-
ImageTemplate: o.ImageTemplate,
60+
ImageTemplate: variable.NewDefaultImageTemplate(),
5661
})
5762
case networkdiags.NetworkDiagnosticName:
58-
diagnostics = append(diagnostics, &networkdiags.NetworkDiagnostic{
59-
KubeClient: kubeClient,
60-
NetNamespacesClient: networkClient.Network(),
61-
ClusterNetworkClient: networkClient.Network(),
62-
ClientFlags: o.ClientFlags,
63-
Level: o.LogOptions.Level,
64-
Factory: o.Factory,
65-
PreventModification: o.PreventModification,
66-
LogDir: o.NetworkOptions.LogDir,
67-
PodImage: o.NetworkOptions.PodImage,
68-
TestPodImage: o.NetworkOptions.TestPodImage,
69-
TestPodProtocol: o.NetworkOptions.TestPodProtocol,
70-
TestPodPort: o.NetworkOptions.TestPodPort,
71-
})
63+
nd := o.ParameterizedDiagnostics[diagnosticName].(*networkdiags.NetworkDiagnostic)
64+
nd.KubeClient = kubeClient
65+
nd.NetNamespacesClient = networkClient.Network()
66+
nd.ClusterNetworkClient = networkClient.Network()
67+
nd.ClientFlags = o.ClientFlags
68+
nd.Level = o.LogOptions.Level
69+
nd.Factory = o.Factory
70+
nd.PreventModification = o.PreventModification
71+
diagnostics = append(diagnostics, nd)
7272
default:
7373
return nil, false, fmt.Errorf("unknown diagnostic: %v", diagnosticName)
7474
}

pkg/oc/admin/diagnostics/cluster.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,27 @@ import (
2525
"k8s.io/kubernetes/pkg/apis/authorization"
2626
)
2727

28-
var (
29-
// availableClusterDiagnostics contains the names of cluster diagnostics that can be executed
30-
// during a single run of diagnostics. Add more diagnostics to the list as they are defined.
31-
availableClusterDiagnostics = sets.NewString(
32-
agldiags.AggregatedLoggingName,
33-
clustdiags.ClusterRegistryName,
34-
clustdiags.ClusterRouterName,
35-
clustdiags.ClusterRolesName,
36-
clustdiags.ClusterRoleBindingsName,
37-
clustdiags.MasterNodeName,
38-
clustdiags.MetricsApiProxyName,
39-
clustdiags.NodeDefinitionsName,
40-
clustdiags.RouteCertificateValidationName,
41-
clustdiags.ServiceExternalIPsName,
42-
)
43-
)
28+
// availableClusterDiagnostics contains the names of cluster diagnostics that can be executed
29+
// during a single run of diagnostics. Add more diagnostics to the list as they are defined.
30+
func availableClusterDiagnostics() types.DiagnosticList {
31+
return types.DiagnosticList{
32+
&agldiags.AggregatedLogging{},
33+
&clustdiags.ClusterRegistry{},
34+
&clustdiags.ClusterRouter{},
35+
&clustdiags.ClusterRoles{},
36+
&clustdiags.ClusterRoleBindings{},
37+
&clustdiags.MasterNode{},
38+
&clustdiags.MetricsApiProxy{},
39+
&clustdiags.NodeDefinitions{},
40+
&clustdiags.RouteCertificateValidation{},
41+
&clustdiags.ServiceExternalIPs{},
42+
}
43+
}
4444

4545
// buildClusterDiagnostics builds cluster Diagnostic objects if a cluster-admin client can be extracted from the rawConfig passed in.
4646
// Returns the Diagnostics built, "ok" bool for whether to proceed or abort, and an error if any was encountered during the building of diagnostics.) {
47-
func (o DiagnosticsOptions) buildClusterDiagnostics(rawConfig *clientcmdapi.Config) ([]types.Diagnostic, bool, error) {
48-
requestedDiagnostics := availableClusterDiagnostics.Intersection(sets.NewString(o.RequestedDiagnostics...)).List()
47+
func (o DiagnosticsConfig) buildClusterDiagnostics(rawConfig *clientcmdapi.Config) ([]types.Diagnostic, bool, error) {
48+
requestedDiagnostics := availableClusterDiagnostics().Names().Intersection(sets.NewString(o.RequestedDiagnostics.List()...)).List()
4949
if len(requestedDiagnostics) == 0 { // no diagnostics to run here
5050
return nil, true, nil // don't waste time on discovery
5151
}
@@ -124,7 +124,7 @@ func (o DiagnosticsOptions) buildClusterDiagnostics(rawConfig *clientcmdapi.Conf
124124
}
125125

126126
// attempts to find which context in the config might be a cluster-admin for the server in the current context.
127-
func (o DiagnosticsOptions) findClusterClients(rawConfig *clientcmdapi.Config) (*rest.Config, kclientset.Interface, bool, string, error) {
127+
func (o DiagnosticsConfig) findClusterClients(rawConfig *clientcmdapi.Config) (*rest.Config, kclientset.Interface, bool, string, error) {
128128
if o.ClientClusterContext != "" { // user has specified cluster context to use
129129
if context, exists := rawConfig.Contexts[o.ClientClusterContext]; !exists {
130130
configErr := fmt.Errorf("Specified '%s' as cluster-admin context, but it was not found in your client configuration.", o.ClientClusterContext)
@@ -160,7 +160,7 @@ func (o DiagnosticsOptions) findClusterClients(rawConfig *clientcmdapi.Config) (
160160
}
161161

162162
// makes the client from the specified context and determines whether it is a cluster-admin.
163-
func (o DiagnosticsOptions) makeClusterClients(rawConfig *clientcmdapi.Config, contextName string, context *clientcmdapi.Context) (*rest.Config, kclientset.Interface, bool, string, error) {
163+
func (o DiagnosticsConfig) makeClusterClients(rawConfig *clientcmdapi.Config, contextName string, context *clientcmdapi.Context) (*rest.Config, kclientset.Interface, bool, string, error) {
164164
overrides := &clientcmd.ConfigOverrides{Context: *context}
165165
clientConfig := clientcmd.NewDefaultClientConfig(*rawConfig, overrides)
166166
serverUrl := rawConfig.Clusters[context.Cluster].Server

pkg/oc/admin/diagnostics/config.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@ import (
1111
)
1212

1313
// determine if we even have a client config
14-
func (o DiagnosticsOptions) detectClientConfig() (bool, []types.DiagnosticError, []types.DiagnosticError) {
14+
func (o DiagnosticsConfig) detectClientConfig() (bool, bool, []types.DiagnosticError, []types.DiagnosticError) {
15+
if o.ClientFlags == nil {
16+
return false, false, []types.DiagnosticError{}, []types.DiagnosticError{}
17+
}
1518
diagnostic := &clientdiagnostics.ConfigLoading{ConfFlagName: config.OpenShiftConfigFlagName, ClientFlags: o.ClientFlags}
1619
o.Logger.Notice("CED2011", "Determining if client configuration exists for client/cluster diagnostics")
1720
result := diagnostic.Check()
1821
for _, entry := range result.Logs() {
1922
o.Logger.LogEntry(entry)
2023
}
21-
return diagnostic.SuccessfulLoad(), result.Warnings(), result.Errors()
24+
return true, diagnostic.SuccessfulLoad(), result.Warnings(), result.Errors()
2225
}
2326

2427
// use the base factory to return a raw config (not specific to a context)
25-
func (o DiagnosticsOptions) buildRawConfig() (*clientcmdapi.Config, error) {
28+
func (o DiagnosticsConfig) buildRawConfig() (*clientcmdapi.Config, error) {
2629
kubeConfig, configErr := o.Factory.OpenShiftClientConfig().RawConfig()
2730
if configErr != nil {
2831
return nil, configErr

0 commit comments

Comments
 (0)