/
diagnostic.go
235 lines (198 loc) · 8.72 KB
/
diagnostic.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package aggregated_logging
import (
"errors"
"fmt"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
kapi "k8s.io/kubernetes/pkg/apis/core"
kapisext "k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/apis/rbac"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
rbacclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/rbac/internalversion"
appsv1 "github.com/openshift/api/apps/v1"
appstypedclient "github.com/openshift/client-go/apps/clientset/versioned/typed/apps/v1"
oauthtypedclient "github.com/openshift/origin/pkg/oauth/generated/internalclientset/typed/oauth/internalversion"
"github.com/openshift/origin/pkg/oc/cli/admin/diagnostics/diagnostics/log"
"github.com/openshift/origin/pkg/oc/cli/admin/diagnostics/diagnostics/types"
projecttypedclient "github.com/openshift/origin/pkg/project/generated/internalclientset/typed/project/internalversion"
routesapi "github.com/openshift/origin/pkg/route/apis/route"
routetypedclient "github.com/openshift/origin/pkg/route/generated/internalclientset/typed/route/internalversion"
securityapi "github.com/openshift/origin/pkg/security/apis/security"
securitytypedclient "github.com/openshift/origin/pkg/security/generated/internalclientset/typed/security/internalversion"
kerrors "k8s.io/apimachinery/pkg/api/errors"
)
// AggregatedLogging is a Diagnostic to check the configurations
// and general integration of the OpenShift stack
// for aggregating container logs
// https://github.com/openshift/origin-aggregated-logging
type AggregatedLogging struct {
Project string
OAuthClientClient oauthtypedclient.OAuthClientsGetter
ProjectClient projecttypedclient.ProjectsGetter
RouteClient routetypedclient.RoutesGetter
CRBClient rbacclient.ClusterRoleBindingsGetter
DCClient appstypedclient.DeploymentConfigsGetter
SCCClient securitytypedclient.SecurityContextConstraintsGetter
KubeClient kclientset.Interface
result types.DiagnosticResult
}
const (
AggregatedLoggingName = "AggregatedLogging"
loggingInfraKey = "logging-infra"
componentKey = "component"
providerKey = "provider"
openshiftValue = "openshift"
fluentdServiceAccountName = "aggregated-logging-fluentd"
flagLoggingProject = "logging-project"
)
var loggingSelector = labels.Set{loggingInfraKey: "support"}
var defaultLoggingProjects = []string{"openshift-logging", "logging"}
//NewAggregatedLogging returns the AggregatedLogging Diagnostic
func NewAggregatedLogging(
project string,
kclient kclientset.Interface,
oauthClientClient oauthtypedclient.OAuthClientsGetter,
projectClient projecttypedclient.ProjectsGetter,
routeClient routetypedclient.RoutesGetter,
crbClient rbacclient.ClusterRoleBindingsGetter,
dcClient appstypedclient.DeploymentConfigsGetter,
sccClient securitytypedclient.SecurityContextConstraintsGetter,
) *AggregatedLogging {
return &AggregatedLogging{
Project: project,
OAuthClientClient: oauthClientClient,
ProjectClient: projectClient,
RouteClient: routeClient,
CRBClient: crbClient,
DCClient: dcClient,
SCCClient: sccClient,
KubeClient: kclient,
result: types.NewDiagnosticResult(AggregatedLoggingName),
}
}
func (d *AggregatedLogging) getScc(name string) (*securityapi.SecurityContextConstraints, error) {
return d.SCCClient.SecurityContextConstraints().Get(name, metav1.GetOptions{})
}
func (d *AggregatedLogging) listClusterRoleBindings() (*rbac.ClusterRoleBindingList, error) {
return d.CRBClient.ClusterRoleBindings().List(metav1.ListOptions{})
}
func (d *AggregatedLogging) routes(project string, options metav1.ListOptions) (*routesapi.RouteList, error) {
return d.RouteClient.Routes(project).List(options)
}
func (d *AggregatedLogging) serviceAccounts(project string, options metav1.ListOptions) (*kapi.ServiceAccountList, error) {
return d.KubeClient.Core().ServiceAccounts(project).List(options)
}
func (d *AggregatedLogging) services(project string, options metav1.ListOptions) (*kapi.ServiceList, error) {
return d.KubeClient.Core().Services(project).List(options)
}
func (d *AggregatedLogging) endpointsForService(project string, service string) (*kapi.Endpoints, error) {
return d.KubeClient.Core().Endpoints(project).Get(service, metav1.GetOptions{})
}
func (d *AggregatedLogging) daemonsets(project string, options metav1.ListOptions) (*kapisext.DaemonSetList, error) {
return d.KubeClient.Extensions().DaemonSets(project).List(metav1.ListOptions{LabelSelector: loggingInfraFluentdSelector.AsSelector().String()})
}
func (d *AggregatedLogging) nodes(options metav1.ListOptions) (*kapi.NodeList, error) {
return d.KubeClient.Core().Nodes().List(metav1.ListOptions{})
}
func (d *AggregatedLogging) pods(project string, options metav1.ListOptions) (*kapi.PodList, error) {
return d.KubeClient.Core().Pods(project).List(options)
}
func (d *AggregatedLogging) deploymentconfigs(project string, options metav1.ListOptions) (*appsv1.DeploymentConfigList, error) {
return d.DCClient.DeploymentConfigs(project).List(options)
}
func (d *AggregatedLogging) Info(id string, message string) {
d.result.Info(id, message)
}
func (d *AggregatedLogging) Error(id string, err error, message string) {
d.result.Error(id, err, message)
}
func (d *AggregatedLogging) Debug(id string, message string) {
d.result.Debug(id, message)
}
func (d *AggregatedLogging) Warn(id string, err error, message string) {
d.result.Warn(id, err, message)
}
func (d *AggregatedLogging) Name() string {
return AggregatedLoggingName
}
func (d *AggregatedLogging) Description() string {
return "Check aggregated logging integration for proper configuration"
}
func (d *AggregatedLogging) Requirements() (client bool, host bool) {
return true, false
}
func (d *AggregatedLogging) Complete(logger *log.Logger) error {
if len(d.Project) > 0 {
return nil
}
// Check if any of the default logging projects are present in the cluster
for _, project := range defaultLoggingProjects {
d.Debug("AGL0031", fmt.Sprintf("Trying default logging project %q", project))
_, err := d.ProjectClient.Projects().Get(project, metav1.GetOptions{})
if err != nil {
if kerrors.IsNotFound(err) {
d.Debug("AGL0032", fmt.Sprintf("Project %q not found", project))
continue
}
d.Error("AGL0034", err, fmt.Sprintf("Fetching project %q returned with error", project))
return nil
}
d.Debug("AGL0033", fmt.Sprintf("Found default logging project %q", project))
d.Project = project
return nil
}
//tried to complete here but no known logging project exists, will be checked in CanRun()
return nil
}
func (d *AggregatedLogging) CanRun() (bool, error) {
if len(d.Project) == 0 {
return false, errors.New("Logging project does not exist")
}
if d.OAuthClientClient == nil || d.ProjectClient == nil || d.RouteClient == nil || d.CRBClient == nil || d.DCClient == nil {
return false, errors.New("Config must include a cluster-admin context to run this diagnostic")
}
if d.KubeClient == nil {
return false, errors.New("Config must include a cluster-admin context to run this diagnostic")
}
return true, nil
}
func (d *AggregatedLogging) Check() types.DiagnosticResult {
d.Debug("AGL0015", fmt.Sprintf("Trying diagnostics for project '%s'", d.Project))
p, err := d.ProjectClient.Projects().Get(d.Project, metav1.GetOptions{})
if err != nil {
d.Error("AGL0018", err, fmt.Sprintf("There was an error retrieving project '%s' which is most likely a transient error: %s", d.Project, err))
return d.result
}
nodeSelector, ok := p.ObjectMeta.Annotations["openshift.io/node-selector"]
if !ok || len(nodeSelector) != 0 {
d.Warn("AGL0030", nil, fmt.Sprintf(projectNodeSelectorWarning, d.Project))
}
checkServiceAccounts(d, d, d.Project)
checkClusterRoleBindings(d, d, d.Project)
checkSccs(d, d, d.Project)
checkDeploymentConfigs(d, d, d.Project)
checkDaemonSets(d, d, d.Project)
checkServices(d, d, d.Project)
checkRoutes(d, d, d.Project)
checkKibana(d, d.RouteClient, d.OAuthClientClient, d.KubeClient, d.Project)
return d.result
}
func (d *AggregatedLogging) AvailableParameters() []types.Parameter {
return []types.Parameter{
{
Name: flagLoggingProject,
Description: fmt.Sprintf("Project that has deployed aggregated logging. Default projects: %s", strings.Join(defaultLoggingProjects, " or ")),
Target: &d.Project,
Default: "",
},
}
}
const projectNodeSelectorWarning = `
The project '%[1]s' was found with either a missing or non-empty node selector annotation.
This could keep Fluentd from running on certain nodes and collecting logs from the entire cluster.
You can correct it by editing the project:
$ oc edit namespace %[1]s
and updating the annotation:
'openshift.io/node-selector' : ""
`