Skip to content

Commit

Permalink
Replace ref package with k8s.io/utils/ptr (#6189)
Browse files Browse the repository at this point in the history
Closes #6163.

Signed-off-by: lubronzhan <lubronzhan@gmail.com>
  • Loading branch information
lubronzhan authored Feb 14, 2024
1 parent bc89948 commit 6e6d2f7
Show file tree
Hide file tree
Showing 82 changed files with 1,137 additions and 1,173 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 @@ -804,7 +804,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) {
customTags = append(customTags, &xdscache_v3.CustomTag{
TagName: "podName",
EnvironmentName: "HOSTNAME",
Expand All @@ -822,16 +822,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)
if err != nil || overallSampling == 0 {
overallSampling = 100.0
}

return &xdscache_v3.TracingConfig{
ServiceName: ref.Val(tracingConfig.ServiceName, "contour"),
ServiceName: ptr.Deref(tracingConfig.ServiceName, "contour"),
ExtensionServiceConfig: extensionSvcConfig,
OverallSampling: overallSampling,
MaxPathTagLength: ref.Val(tracingConfig.MaxPathTagLength, 256),
MaxPathTagLength: ptr.Deref(tracingConfig.MaxPathTagLength, 256),
CustomTags: customTags,
}, nil
}
Expand All @@ -848,11 +848,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,

FailOpen: ptr.Deref(contourConfiguration.RateLimitService.FailOpen, false),
EnableXRateLimitHeaders: ptr.Deref(contourConfiguration.RateLimitService.EnableXRateLimitHeaders, false),
EnableResourceExhaustedCode: ptr.Deref(contourConfiguration.RateLimitService.EnableResourceExhaustedCode, false),
}, 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 @@ -345,25 +345,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)
}
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)
}
if len(ctx.Config.Timeouts.MaxConnectionDuration) > 0 {
timeoutParams.MaxConnectionDuration = ref.To(ctx.Config.Timeouts.MaxConnectionDuration)
timeoutParams.MaxConnectionDuration = ptr.To(ctx.Config.Timeouts.MaxConnectionDuration)
}
if len(ctx.Config.Timeouts.DelayedCloseTimeout) > 0 {
timeoutParams.DelayedCloseTimeout = ref.To(ctx.Config.Timeouts.DelayedCloseTimeout)
timeoutParams.DelayedCloseTimeout = ptr.To(ctx.Config.Timeouts.DelayedCloseTimeout)
}
if len(ctx.Config.Timeouts.ConnectionShutdownGracePeriod) > 0 {
timeoutParams.ConnectionShutdownGracePeriod = ref.To(ctx.Config.Timeouts.ConnectionShutdownGracePeriod)
timeoutParams.ConnectionShutdownGracePeriod = ptr.To(ctx.Config.Timeouts.ConnectionShutdownGracePeriod)
}
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 @@ -412,9 +412,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 @@ -466,7 +466,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 @@ -610,7 +610,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 @@ -651,9 +651,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 @@ -751,7 +751,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 @@ -828,10 +828,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 @@ -848,10 +848,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 @@ -889,15 +889,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 6e6d2f7

Please sign in to comment.