Skip to content

Commit

Permalink
Replace ref package with k8s.io/utils/ptr
Browse files Browse the repository at this point in the history
Signed-off-by: lubronzhan <[email protected]>
  • Loading branch information
lubronzhan committed Feb 13, 2024
1 parent bd003fb commit 718f220
Show file tree
Hide file tree
Showing 83 changed files with 1,145 additions and 1,181 deletions.
6 changes: 3 additions & 3 deletions apis/projectcontour/v1alpha1/contourconfig_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/utils/ptr"

contour_v1alpha1 "github.com/projectcontour/contour/apis/projectcontour/v1alpha1"
"github.com/projectcontour/contour/internal/ref"
)

func TestContourConfigurationSpecValidate(t *testing.T) {
Expand Down Expand Up @@ -193,10 +193,10 @@ func TestContourConfigurationSpecValidate(t *testing.T) {
}
require.NoError(t, c.Validate())

c.Tracing.OverallSampling = ref.To("number")
c.Tracing.OverallSampling = ptr.To("number")
require.Error(t, c.Validate())

c.Tracing.OverallSampling = ref.To("10")
c.Tracing.OverallSampling = ptr.To("10")
require.NoError(t, c.Validate())

customTags := []*contour_v1alpha1.CustomTag{
Expand Down
21 changes: 11 additions & 10 deletions cmd/contour/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/utils/ptr"
ctrl_cache "sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
Expand All @@ -59,7 +60,6 @@ import (
"github.com/projectcontour/contour/internal/k8s"
"github.com/projectcontour/contour/internal/leadership"
"github.com/projectcontour/contour/internal/metrics"
"github.com/projectcontour/contour/internal/ref"
"github.com/projectcontour/contour/internal/timeout"
"github.com/projectcontour/contour/internal/xds"
contour_xds_v3 "github.com/projectcontour/contour/internal/xds/v3"
Expand Down Expand Up @@ -819,7 +819,7 @@ func (s *Server) setupTracingService(tracingConfig *contour_v1alpha1.TracingConf

var customTags []*xdscache_v3.CustomTag

if ref.Val(tracingConfig.IncludePodDetail, true) {
if ptr.Deref(tracingConfig.IncludePodDetail, true) {

Check warning on line 822 in cmd/contour/serve.go

View check run for this annotation

Codecov / codecov/patch

cmd/contour/serve.go#L822

Added line #L822 was not covered by tests
customTags = append(customTags, &xdscache_v3.CustomTag{
TagName: "podName",
EnvironmentName: "HOSTNAME",
Expand All @@ -837,16 +837,16 @@ func (s *Server) setupTracingService(tracingConfig *contour_v1alpha1.TracingConf
})
}

overallSampling, err := strconv.ParseFloat(ref.Val(tracingConfig.OverallSampling, "100"), 64)
overallSampling, err := strconv.ParseFloat(ptr.Deref(tracingConfig.OverallSampling, "100"), 64)

Check warning on line 840 in cmd/contour/serve.go

View check run for this annotation

Codecov / codecov/patch

cmd/contour/serve.go#L840

Added line #L840 was not covered by tests
if err != nil || overallSampling == 0 {
overallSampling = 100.0
}

return &xdscache_v3.TracingConfig{
ServiceName: ref.Val(tracingConfig.ServiceName, "contour"),
ServiceName: ptr.Deref(tracingConfig.ServiceName, "contour"),

Check warning on line 846 in cmd/contour/serve.go

View check run for this annotation

Codecov / codecov/patch

cmd/contour/serve.go#L846

Added line #L846 was not covered by tests
ExtensionServiceConfig: extensionSvcConfig,
OverallSampling: overallSampling,
MaxPathTagLength: ref.Val(tracingConfig.MaxPathTagLength, 256),
MaxPathTagLength: ptr.Deref(tracingConfig.MaxPathTagLength, 256),

Check warning on line 849 in cmd/contour/serve.go

View check run for this annotation

Codecov / codecov/patch

cmd/contour/serve.go#L849

Added line #L849 was not covered by tests
CustomTags: customTags,
}, nil
}
Expand All @@ -863,11 +863,12 @@ func (s *Server) setupRateLimitService(contourConfiguration contour_v1alpha1.Con
}

return &xdscache_v3.RateLimitConfig{
ExtensionServiceConfig: extensionSvcConfig,
Domain: contourConfiguration.RateLimitService.Domain,
FailOpen: ref.Val(contourConfiguration.RateLimitService.FailOpen, false),
EnableXRateLimitHeaders: ref.Val(contourConfiguration.RateLimitService.EnableXRateLimitHeaders, false),
EnableResourceExhaustedCode: ref.Val(contourConfiguration.RateLimitService.EnableResourceExhaustedCode, false),
ExtensionServiceConfig: extensionSvcConfig,
Domain: contourConfiguration.RateLimitService.Domain,

Check warning on line 867 in cmd/contour/serve.go

View check run for this annotation

Codecov / codecov/patch

cmd/contour/serve.go#L866-L867

Added lines #L866 - L867 were not covered by tests

FailOpen: ptr.Deref(contourConfiguration.RateLimitService.FailOpen, false),
EnableXRateLimitHeaders: ptr.Deref(contourConfiguration.RateLimitService.EnableXRateLimitHeaders, false),
EnableResourceExhaustedCode: ptr.Deref(contourConfiguration.RateLimitService.EnableResourceExhaustedCode, false),

Check warning on line 871 in cmd/contour/serve.go

View check run for this annotation

Codecov / codecov/patch

cmd/contour/serve.go#L869-L871

Added lines #L869 - L871 were not covered by tests
}, nil
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/contour/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"

contour_v1alpha1 "github.com/projectcontour/contour/apis/projectcontour/v1alpha1"
"github.com/projectcontour/contour/internal/dag"
"github.com/projectcontour/contour/internal/ref"
)

func TestGetDAGBuilder(t *testing.T) {
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestGetDAGBuilder(t *testing.T) {
},
Remove: []string{"res-remove-key-1", "res-remove-key-2"},
},
ApplyToIngress: ref.To(false),
ApplyToIngress: ptr.To(false),
}

serve := &Server{
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestGetDAGBuilder(t *testing.T) {
},
Remove: []string{"res-remove-key-1", "res-remove-key-2"},
},
ApplyToIngress: ref.To(true),
ApplyToIngress: ptr.To(true),
}

serve := &Server{
Expand Down
26 changes: 13 additions & 13 deletions cmd/contour/servecontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
"k8s.io/utils/ptr"

contour_v1 "github.com/projectcontour/contour/apis/projectcontour/v1"
contour_v1alpha1 "github.com/projectcontour/contour/apis/projectcontour/v1alpha1"
envoy_v3 "github.com/projectcontour/contour/internal/envoy/v3"
"github.com/projectcontour/contour/internal/k8s"
"github.com/projectcontour/contour/internal/ref"
xdscache_v3 "github.com/projectcontour/contour/internal/xdscache/v3"
"github.com/projectcontour/contour/pkg/config"
)
Expand Down Expand Up @@ -167,7 +167,7 @@ func grpcOptions(log logrus.FieldLogger, contourXDSConfig *contour_v1alpha1.TLS)
}),
}

if !ref.Val(contourXDSConfig.Insecure, false) {
if !ptr.Deref(contourXDSConfig.Insecure, false) {
tlsconfig := tlsconfig(log, contourXDSConfig)
creds := credentials.NewTLS(tlsconfig)
opts = append(opts, grpc.Creds(creds))
Expand Down Expand Up @@ -350,25 +350,25 @@ func (ctx *serveContext) convertToContourConfigurationSpec() contour_v1alpha1.Co

timeoutParams := &contour_v1alpha1.TimeoutParameters{}
if len(ctx.Config.Timeouts.RequestTimeout) > 0 {
timeoutParams.RequestTimeout = ref.To(ctx.Config.Timeouts.RequestTimeout)
timeoutParams.RequestTimeout = ptr.To(ctx.Config.Timeouts.RequestTimeout)

Check warning on line 353 in cmd/contour/servecontext.go

View check run for this annotation

Codecov / codecov/patch

cmd/contour/servecontext.go#L353

Added line #L353 was not covered by tests
}
if len(ctx.Config.Timeouts.ConnectionIdleTimeout) > 0 {
timeoutParams.ConnectionIdleTimeout = ref.To(ctx.Config.Timeouts.ConnectionIdleTimeout)
timeoutParams.ConnectionIdleTimeout = ptr.To(ctx.Config.Timeouts.ConnectionIdleTimeout)
}
if len(ctx.Config.Timeouts.StreamIdleTimeout) > 0 {
timeoutParams.StreamIdleTimeout = ref.To(ctx.Config.Timeouts.StreamIdleTimeout)
timeoutParams.StreamIdleTimeout = ptr.To(ctx.Config.Timeouts.StreamIdleTimeout)

Check warning on line 359 in cmd/contour/servecontext.go

View check run for this annotation

Codecov / codecov/patch

cmd/contour/servecontext.go#L359

Added line #L359 was not covered by tests
}
if len(ctx.Config.Timeouts.MaxConnectionDuration) > 0 {
timeoutParams.MaxConnectionDuration = ref.To(ctx.Config.Timeouts.MaxConnectionDuration)
timeoutParams.MaxConnectionDuration = ptr.To(ctx.Config.Timeouts.MaxConnectionDuration)

Check warning on line 362 in cmd/contour/servecontext.go

View check run for this annotation

Codecov / codecov/patch

cmd/contour/servecontext.go#L362

Added line #L362 was not covered by tests
}
if len(ctx.Config.Timeouts.DelayedCloseTimeout) > 0 {
timeoutParams.DelayedCloseTimeout = ref.To(ctx.Config.Timeouts.DelayedCloseTimeout)
timeoutParams.DelayedCloseTimeout = ptr.To(ctx.Config.Timeouts.DelayedCloseTimeout)

Check warning on line 365 in cmd/contour/servecontext.go

View check run for this annotation

Codecov / codecov/patch

cmd/contour/servecontext.go#L365

Added line #L365 was not covered by tests
}
if len(ctx.Config.Timeouts.ConnectionShutdownGracePeriod) > 0 {
timeoutParams.ConnectionShutdownGracePeriod = ref.To(ctx.Config.Timeouts.ConnectionShutdownGracePeriod)
timeoutParams.ConnectionShutdownGracePeriod = ptr.To(ctx.Config.Timeouts.ConnectionShutdownGracePeriod)

Check warning on line 368 in cmd/contour/servecontext.go

View check run for this annotation

Codecov / codecov/patch

cmd/contour/servecontext.go#L368

Added line #L368 was not covered by tests
}
if len(ctx.Config.Timeouts.ConnectTimeout) > 0 {
timeoutParams.ConnectTimeout = ref.To(ctx.Config.Timeouts.ConnectTimeout)
timeoutParams.ConnectTimeout = ptr.To(ctx.Config.Timeouts.ConnectTimeout)
}

var dnsLookupFamily contour_v1alpha1.ClusterDNSFamilyType
Expand Down Expand Up @@ -417,9 +417,9 @@ func (ctx *serveContext) convertToContourConfigurationSpec() contour_v1alpha1.Co
Namespace: nsedName.Namespace,
},
Domain: ctx.Config.RateLimitService.Domain,
FailOpen: ref.To(ctx.Config.RateLimitService.FailOpen),
EnableXRateLimitHeaders: ref.To(ctx.Config.RateLimitService.EnableXRateLimitHeaders),
EnableResourceExhaustedCode: ref.To(ctx.Config.RateLimitService.EnableResourceExhaustedCode),
FailOpen: ptr.To(ctx.Config.RateLimitService.FailOpen),
EnableXRateLimitHeaders: ptr.To(ctx.Config.RateLimitService.EnableXRateLimitHeaders),
EnableResourceExhaustedCode: ptr.To(ctx.Config.RateLimitService.EnableResourceExhaustedCode),
DefaultGlobalRateLimitPolicy: ctx.Config.RateLimitService.DefaultGlobalRateLimitPolicy,
}
}
Expand Down Expand Up @@ -471,7 +471,7 @@ func (ctx *serveContext) convertToContourConfigurationSpec() contour_v1alpha1.Co
Set: ctx.Config.Policy.ResponseHeadersPolicy.Set,
Remove: ctx.Config.Policy.ResponseHeadersPolicy.Remove,
},
ApplyToIngress: ref.To(ctx.Config.Policy.ApplyToIngress),
ApplyToIngress: ptr.To(ctx.Config.Policy.ApplyToIngress),
}

var clientCertificate *contour_v1alpha1.NamespacedName
Expand Down
72 changes: 36 additions & 36 deletions cmd/contour/servecontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import (
"github.com/stretchr/testify/assert"
"github.com/tsaarni/certyaml"
"google.golang.org/grpc"
"k8s.io/utils/ptr"

contour_v1 "github.com/projectcontour/contour/apis/projectcontour/v1"
contour_v1alpha1 "github.com/projectcontour/contour/apis/projectcontour/v1alpha1"
envoy_v3 "github.com/projectcontour/contour/internal/envoy/v3"
"github.com/projectcontour/contour/internal/fixture"
"github.com/projectcontour/contour/internal/ref"
"github.com/projectcontour/contour/pkg/config"
)

Expand Down Expand Up @@ -93,15 +93,15 @@ func TestServeContextTLSParams(t *testing.T) {
CAFile: "cacert.pem",
CertFile: "contourcert.pem",
KeyFile: "contourkey.pem",
Insecure: ref.To(false),
Insecure: ptr.To(false),
},
expectError: false,
},
"tls partially supplied": {
tls: &contour_v1alpha1.TLS{
CertFile: "contourcert.pem",
KeyFile: "contourkey.pem",
Insecure: ref.To(false),
Insecure: ptr.To(false),
},
expectError: true,
},
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestServeContextCertificateHandling(t *testing.T) {
CAFile: filepath.Join(configDir, "CAcert.pem"),
CertFile: filepath.Join(configDir, "contourcert.pem"),
KeyFile: filepath.Join(configDir, "contourkey.pem"),
Insecure: ref.To(false),
Insecure: ptr.To(false),
}

// Initial set of credentials must be written into temp directory before
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestTlsVersionDeprecation(t *testing.T) {
CAFile: filepath.Join(configDir, "CAcert.pem"),
CertFile: filepath.Join(configDir, "contourcert.pem"),
KeyFile: filepath.Join(configDir, "contourkey.pem"),
Insecure: ref.To(false),
Insecure: ptr.To(false),
}

err = caCert.WritePEM(contourTLS.CAFile, filepath.Join(configDir, "CAkey.pem"))
Expand Down Expand Up @@ -386,7 +386,7 @@ func TestConvertServeContext(t *testing.T) {
CAFile: "/certs/ca.crt",
CertFile: "/certs/cert.crt",
KeyFile: "/certs/cert.key",
Insecure: ref.To(false),
Insecure: ptr.To(false),
},
},
Ingress: &contour_v1alpha1.IngressConfig{
Expand All @@ -407,9 +407,9 @@ func TestConvertServeContext(t *testing.T) {
Namespace: "projectcontour",
},
Listener: &contour_v1alpha1.EnvoyListenerConfig{
UseProxyProto: ref.To(false),
DisableAllowChunkedLength: ref.To(false),
DisableMergeSlashes: ref.To(false),
UseProxyProto: ptr.To(false),
DisableAllowChunkedLength: ptr.To(false),
DisableMergeSlashes: ptr.To(false),
ServerHeaderTransformation: contour_v1alpha1.OverwriteServerHeader,
TLS: &contour_v1alpha1.EnvoyTLS{
MinimumProtocolVersion: "",
Expand Down Expand Up @@ -471,8 +471,8 @@ func TestConvertServeContext(t *testing.T) {
},
DefaultHTTPVersions: nil,
Timeouts: &contour_v1alpha1.TimeoutParameters{
ConnectionIdleTimeout: ref.To("60s"),
ConnectTimeout: ref.To("2s"),
ConnectionIdleTimeout: ptr.To("60s"),
ConnectTimeout: ptr.To("2s"),
},
Cluster: &contour_v1alpha1.ClusterParameters{
DNSLookupFamily: contour_v1alpha1.AutoClusterDNSFamily,
Expand All @@ -483,22 +483,22 @@ func TestConvertServeContext(t *testing.T) {
},
},
Network: &contour_v1alpha1.NetworkParameters{
EnvoyAdminPort: ref.To(9001),
XffNumTrustedHops: ref.To(uint32(0)),
EnvoyAdminPort: ptr.To(9001),
XffNumTrustedHops: ptr.To(uint32(0)),
},
},
Gateway: nil,
HTTPProxy: &contour_v1alpha1.HTTPProxyConfig{
DisablePermitInsecure: ref.To(false),
DisablePermitInsecure: ptr.To(false),
FallbackCertificate: nil,
},
EnableExternalNameService: ref.To(false),
EnableExternalNameService: ptr.To(false),
RateLimitService: nil,
GlobalExternalAuthorization: nil,
Policy: &contour_v1alpha1.PolicyConfig{
RequestHeadersPolicy: &contour_v1alpha1.HeadersPolicy{},
ResponseHeadersPolicy: &contour_v1alpha1.HeadersPolicy{},
ApplyToIngress: ref.To(false),
ApplyToIngress: ptr.To(false),
},
Metrics: &contour_v1alpha1.MetricsConfig{
Address: "0.0.0.0",
Expand Down Expand Up @@ -544,7 +544,7 @@ func TestConvertServeContext(t *testing.T) {
Set: map[string]string{"custom-response-header-set": "foo-bar", "Host": "response-bar.com"},
Remove: []string{"custom-response-header-remove"},
},
ApplyToIngress: ref.To(true),
ApplyToIngress: ptr.To(true),
}
return cfg
},
Expand Down Expand Up @@ -624,7 +624,7 @@ func TestConvertServeContext(t *testing.T) {
},
getContourConfiguration: func(cfg contour_v1alpha1.ContourConfigurationSpec) contour_v1alpha1.ContourConfigurationSpec {
cfg.HTTPProxy = &contour_v1alpha1.HTTPProxyConfig{
DisablePermitInsecure: ref.To(true),
DisablePermitInsecure: ptr.To(true),
FallbackCertificate: &contour_v1alpha1.NamespacedName{
Name: "fallbackname",
Namespace: "fallbacknamespace",
Expand Down Expand Up @@ -665,9 +665,9 @@ func TestConvertServeContext(t *testing.T) {
Namespace: "ratens",
},
Domain: "contour",
FailOpen: ref.To(true),
EnableXRateLimitHeaders: ref.To(true),
EnableResourceExhaustedCode: ref.To(true),
FailOpen: ptr.To(true),
EnableXRateLimitHeaders: ptr.To(true),
EnableResourceExhaustedCode: ptr.To(true),
DefaultGlobalRateLimitPolicy: &contour_v1.GlobalRateLimitPolicy{
Descriptors: []contour_v1.RateLimitDescriptor{
{
Expand Down Expand Up @@ -765,7 +765,7 @@ func TestConvertServeContext(t *testing.T) {
return ctx
},
getContourConfiguration: func(cfg contour_v1alpha1.ContourConfigurationSpec) contour_v1alpha1.ContourConfigurationSpec {
cfg.Envoy.Listener.DisableMergeSlashes = ref.To(true)
cfg.Envoy.Listener.DisableMergeSlashes = ptr.To(true)
return cfg
},
},
Expand Down Expand Up @@ -842,10 +842,10 @@ func TestConvertServeContext(t *testing.T) {
"tracing config normal": {
getServeContext: func(ctx *serveContext) *serveContext {
ctx.Config.Tracing = &config.Tracing{
IncludePodDetail: ref.To(false),
ServiceName: ref.To("contour"),
OverallSampling: ref.To("100"),
MaxPathTagLength: ref.To(uint32(256)),
IncludePodDetail: ptr.To(false),
ServiceName: ptr.To("contour"),
OverallSampling: ptr.To("100"),
MaxPathTagLength: ptr.To(uint32(256)),
CustomTags: []config.CustomTag{
{
TagName: "literal",
Expand All @@ -862,10 +862,10 @@ func TestConvertServeContext(t *testing.T) {
},
getContourConfiguration: func(cfg contour_v1alpha1.ContourConfigurationSpec) contour_v1alpha1.ContourConfigurationSpec {
cfg.Tracing = &contour_v1alpha1.TracingConfig{
IncludePodDetail: ref.To(false),
ServiceName: ref.To("contour"),
OverallSampling: ref.To("100"),
MaxPathTagLength: ref.To(uint32(256)),
IncludePodDetail: ptr.To(false),
ServiceName: ptr.To("contour"),
OverallSampling: ptr.To("100"),
MaxPathTagLength: ptr.To(uint32(256)),
CustomTags: []*contour_v1alpha1.CustomTag{
{
TagName: "literal",
Expand Down Expand Up @@ -903,15 +903,15 @@ func TestConvertServeContext(t *testing.T) {
},
"envoy listener settings": {
getServeContext: func(ctx *serveContext) *serveContext {
ctx.Config.Listener.MaxRequestsPerIOCycle = ref.To(uint32(10))
ctx.Config.Listener.HTTP2MaxConcurrentStreams = ref.To(uint32(30))
ctx.Config.Listener.MaxConnectionsPerListener = ref.To(uint32(50))
ctx.Config.Listener.MaxRequestsPerIOCycle = ptr.To(uint32(10))
ctx.Config.Listener.HTTP2MaxConcurrentStreams = ptr.To(uint32(30))
ctx.Config.Listener.MaxConnectionsPerListener = ptr.To(uint32(50))
return ctx
},
getContourConfiguration: func(cfg contour_v1alpha1.ContourConfigurationSpec) contour_v1alpha1.ContourConfigurationSpec {
cfg.Envoy.Listener.MaxRequestsPerIOCycle = ref.To(uint32(10))
cfg.Envoy.Listener.HTTP2MaxConcurrentStreams = ref.To(uint32(30))
cfg.Envoy.Listener.MaxConnectionsPerListener = ref.To(uint32(50))
cfg.Envoy.Listener.MaxRequestsPerIOCycle = ptr.To(uint32(10))
cfg.Envoy.Listener.HTTP2MaxConcurrentStreams = ptr.To(uint32(30))
cfg.Envoy.Listener.MaxConnectionsPerListener = ptr.To(uint32(50))
return cfg
},
},
Expand Down
Loading

0 comments on commit 718f220

Please sign in to comment.