Skip to content

Commit 01054ae

Browse files
committed
address review comments
1 parent 8c6f2c4 commit 01054ae

File tree

4 files changed

+97
-22
lines changed

4 files changed

+97
-22
lines changed

cluster/gce/gci/configure-helper.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,6 @@ EOF
999999
cat <<EOF >/etc/srv/kubernetes/opentelemetry_config.yaml
10001000
apiVersion: apiserver.k8s.io/v1alpha1
10011001
kind: OpenTelemetryClientConfiguration
1002-
url: localhost:55680
10031002
EOF
10041003
fi
10051004

staging/src/k8s.io/apiserver/pkg/opentelemetry/config.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ import (
2929
"sigs.k8s.io/yaml"
3030
)
3131

32-
var cfgScheme = runtime.NewScheme()
32+
var (
33+
cfgScheme = runtime.NewScheme()
34+
35+
defaultPort = int32(55680)
36+
defaultURL = "localhost:55680"
37+
)
3338

3439
func init() {
3540
install.Install(cfgScheme)
@@ -38,7 +43,7 @@ func init() {
3843
// ReadOpenTelemetryConfiguration reads the opentelemetry configuration from a file
3944
func ReadOpenTelemetryConfiguration(configFilePath string) (*apiserver.OpenTelemetryClientConfiguration, error) {
4045
if configFilePath == "" {
41-
return nil, nil
46+
return nil, fmt.Errorf("opentelemetry config file was empty")
4247
}
4348
data, err := ioutil.ReadFile(configFilePath)
4449
if err != nil {
@@ -61,6 +66,21 @@ func ReadOpenTelemetryConfiguration(configFilePath string) (*apiserver.OpenTelem
6166
return internalConfig, nil
6267
}
6368

69+
// DefaultOpenTelemetryConfiguration defaults unset fields in the OpenTelemetryClientConfiguration
70+
func DefaultOpenTelemetryConfiguration(config *apiserver.OpenTelemetryClientConfiguration) {
71+
if config == nil {
72+
return
73+
}
74+
// Default the service port to the default OTLP port
75+
if config.Service != nil && config.Service.Port == nil {
76+
config.Service.Port = &defaultPort
77+
}
78+
// If niether URL or service is set, use the default URL
79+
if config.Service == nil && config.URL == nil {
80+
config.URL = &defaultURL
81+
}
82+
}
83+
6484
// ValidateOpenTelemetryConfiguration validates the opentelemetry configuration
6585
func ValidateOpenTelemetryConfiguration(config *apiserver.OpenTelemetryClientConfiguration) field.ErrorList {
6686
allErrs := field.ErrorList{}

staging/src/k8s.io/apiserver/pkg/opentelemetry/config_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,73 @@ spec:
131131
}
132132
}
133133

134+
func TestDefaultOpenTelemetryConfiguration(t *testing.T) {
135+
otherPort := int32(12378)
136+
otherURL := "foo:12345"
137+
testcases := []struct {
138+
name string
139+
expectedURL *string
140+
expectedServicePort *int32
141+
config *apiserver.OpenTelemetryClientConfiguration
142+
}{
143+
{
144+
name: "all-empty",
145+
expectedURL: &defaultURL,
146+
config: &apiserver.OpenTelemetryClientConfiguration{
147+
TypeMeta: metav1.TypeMeta{
148+
Kind: "",
149+
APIVersion: "",
150+
},
151+
},
152+
},
153+
{
154+
name: "empty-service",
155+
expectedServicePort: &defaultPort,
156+
config: &apiserver.OpenTelemetryClientConfiguration{
157+
TypeMeta: metav1.TypeMeta{
158+
Kind: "",
159+
APIVersion: "",
160+
},
161+
Service: &apiserver.ServiceReference{},
162+
},
163+
},
164+
{
165+
name: "existing-url",
166+
expectedURL: &otherURL,
167+
config: &apiserver.OpenTelemetryClientConfiguration{
168+
TypeMeta: metav1.TypeMeta{
169+
Kind: "",
170+
APIVersion: "",
171+
},
172+
URL: &otherURL,
173+
},
174+
},
175+
{
176+
name: "existing-service-port",
177+
expectedServicePort: &otherPort,
178+
config: &apiserver.OpenTelemetryClientConfiguration{
179+
TypeMeta: metav1.TypeMeta{
180+
Kind: "",
181+
APIVersion: "",
182+
},
183+
Service: &apiserver.ServiceReference{Port: &otherPort},
184+
},
185+
},
186+
}
187+
188+
for _, tc := range testcases {
189+
t.Run(tc.name, func(t *testing.T) {
190+
DefaultOpenTelemetryConfiguration(tc.config)
191+
if tc.expectedURL != nil && *tc.expectedURL != *tc.config.URL {
192+
t.Errorf("Calling DefaultOpenTelemetryConfiguration expected URL %v, got %v", *tc.expectedURL, *tc.config.URL)
193+
}
194+
if tc.expectedServicePort != nil && *tc.expectedServicePort != *tc.config.Service.Port {
195+
t.Errorf("Calling DefaultOpenTelemetryConfiguration expected Service.Port %v, got %v", *tc.expectedServicePort, *tc.config.Service.Port)
196+
}
197+
})
198+
}
199+
}
200+
134201
func TestValidateOpenTelemetryConfiguration(t *testing.T) {
135202
port := int32(12378)
136203
testcases := []struct {

staging/src/k8s.io/apiserver/pkg/server/options/opentelemetry.go

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import (
2424
"github.com/spf13/pflag"
2525
"go.opentelemetry.io/otel/exporters/otlp"
2626
"google.golang.org/grpc"
27-
"k8s.io/utils/path"
2827

2928
"k8s.io/apiserver/pkg/opentelemetry"
3029
"k8s.io/apiserver/pkg/server/egressselector"
3130
"k8s.io/component-base/traces"
31+
"k8s.io/utils/path"
3232
)
3333

3434
// OpenTelemetryOptions contain configuration options for opentelemetry
@@ -55,24 +55,21 @@ func (o *OpenTelemetryOptions) AddFlags(fs *pflag.FlagSet) {
5555

5656
// Apply adds the opentelemetry settings to the global configuration.
5757
func (o *OpenTelemetryOptions) Apply(es *egressselector.EgressSelector) error {
58-
if o == nil {
58+
if o == nil || o.ConfigFile == "" {
5959
return nil
6060
}
6161

6262
npConfig, err := opentelemetry.ReadOpenTelemetryConfiguration(o.ConfigFile)
6363
if err != nil {
6464
return fmt.Errorf("failed to read opentelemetry config: %v", err)
6565
}
66+
67+
opentelemetry.DefaultOpenTelemetryConfiguration(npConfig)
6668
errs := opentelemetry.ValidateOpenTelemetryConfiguration(npConfig)
6769
if len(errs) > 0 {
6870
return fmt.Errorf("failed to validate opentelemetry configuration: %v", errs.ToAggregate())
6971
}
7072

71-
if npConfig == nil {
72-
// No config file was specified, so don't enable exporting
73-
return nil
74-
}
75-
7673
opts := []otlp.ExporterOption{}
7774
if npConfig.URL != nil {
7875
opts = append(opts, otlp.WithAddress(*npConfig.URL))
@@ -92,12 +89,7 @@ func (o *OpenTelemetryOptions) Apply(es *egressselector.EgressSelector) error {
9289
}
9390
}
9491
if npConfig.Service != nil {
95-
// Default port is 55680
96-
port := int32(55680)
97-
if npConfig.Service.Port != nil {
98-
port = *npConfig.Service.Port
99-
}
100-
addr := fmt.Sprintf("%s.%s:%d", npConfig.Service.Name, npConfig.Service.Namespace, port)
92+
addr := fmt.Sprintf("%s.%s:%d", npConfig.Service.Name, npConfig.Service.Namespace, *npConfig.Service.Port)
10193
opts = append(opts, otlp.WithAddress(addr))
10294

10395
if es != nil {
@@ -125,16 +117,13 @@ func (o *OpenTelemetryOptions) Apply(es *egressselector.EgressSelector) error {
125117
}
126118

127119
// Validate verifies flags passed to OpenTelemetryOptions.
128-
func (o *OpenTelemetryOptions) Validate() []error {
120+
func (o *OpenTelemetryOptions) Validate() (errs []error) {
129121
if o == nil || o.ConfigFile == "" {
130-
return nil
122+
return
131123
}
132124

133-
errs := []error{}
134-
135125
if exists, err := path.Exists(path.CheckFollowSymlink, o.ConfigFile); !exists || err != nil {
136126
errs = append(errs, fmt.Errorf("opentelemetry-config-file %s does not exist", o.ConfigFile))
137127
}
138-
139-
return errs
128+
return
140129
}

0 commit comments

Comments
 (0)