func renderConfig()

in pkg/backends/features/cdn.go [82:210]


func renderConfig(sp utils.ServicePort) *composite.BackendService {
	cdnConfig := sp.BackendConfig.Spec.Cdn
	be := &composite.BackendService{}
	be.EnableCDN = cdnConfig.Enabled

	if !be.EnableCDN {
		return be
	}

	be.CdnPolicy = &composite.BackendServiceCdnPolicy{}

	if cdnConfig.CachePolicy != nil {
		beCacheKeyPolicy := &composite.CacheKeyPolicy{}
		beCacheKeyPolicy.IncludeHost = cdnConfig.CachePolicy.IncludeHost
		beCacheKeyPolicy.IncludeProtocol = cdnConfig.CachePolicy.IncludeProtocol
		beCacheKeyPolicy.IncludeQueryString = cdnConfig.CachePolicy.IncludeQueryString
		if cdnConfig.CachePolicy.IncludeQueryString {
			// we must check for an empty list here as the user might set  'queryStringBlacklist:[]' and compare with DeepEqual will fail
			if len(cdnConfig.CachePolicy.QueryStringBlacklist) > 0 {
				beCacheKeyPolicy.QueryStringBlacklist = cdnConfig.CachePolicy.QueryStringBlacklist
			}
			// we must check for an empty list here as the user might set  'queryStringWhitelist:[]' and compare with DeepEqual will fail
			if len(cdnConfig.CachePolicy.QueryStringWhitelist) > 0 {
				beCacheKeyPolicy.QueryStringWhitelist = cdnConfig.CachePolicy.QueryStringWhitelist
			}
		}
		be.CdnPolicy.CacheKeyPolicy = beCacheKeyPolicy
	} else {
		be.CdnPolicy.CacheKeyPolicy = defaultCdnPolicy.CacheKeyPolicy
	}

	if cdnConfig.CacheMode != nil {
		be.CdnPolicy.CacheMode = *cdnConfig.CacheMode
	} else {
		be.CdnPolicy.CacheMode = defaultCdnPolicy.CacheMode
	}

	if cdnConfig.RequestCoalescing != nil {
		be.CdnPolicy.RequestCoalescing = *cdnConfig.RequestCoalescing
	} else {
		be.CdnPolicy.RequestCoalescing = defaultCdnPolicy.RequestCoalescing
	}

	if cdnConfig.ServeWhileStale != nil {
		be.CdnPolicy.ServeWhileStale = *cdnConfig.ServeWhileStale
	} else {
		be.CdnPolicy.ServeWhileStale = defaultCdnPolicy.ServeWhileStale
	}

	// MaxTtl must be specified with CACHE_ALL_STATIC cache_mode only
	if be.CdnPolicy.CacheMode != "CACHE_ALL_STATIC" {
		be.CdnPolicy.MaxTtl = 0
	} else if cdnConfig.MaxTtl != nil {
		be.CdnPolicy.MaxTtl = *cdnConfig.MaxTtl
		if be.CdnPolicy.MaxTtl == 0 {
			be.CdnPolicy.ForceSendFields = append(be.CdnPolicy.ForceSendFields, "MaxTtl")
		}
	} else {
		be.CdnPolicy.MaxTtl = defaultCdnPolicy.MaxTtl
	}

	// if USE_ORIGIN_HEADERS ClientTtl must be ignored
	if be.CdnPolicy.CacheMode == "USE_ORIGIN_HEADERS" {
		be.CdnPolicy.ClientTtl = 0
	} else if cdnConfig.ClientTtl != nil {
		be.CdnPolicy.ClientTtl = *cdnConfig.ClientTtl
		if be.CdnPolicy.ClientTtl == 0 {
			be.CdnPolicy.ForceSendFields = append(be.CdnPolicy.ForceSendFields, "ClientTtl")
		}
	} else {
		be.CdnPolicy.ClientTtl = defaultCdnPolicy.ClientTtl
	}

	// if USE_ORIGIN_HEADERS DefaultTtl must be ignored
	if be.CdnPolicy.CacheMode == "USE_ORIGIN_HEADERS" {
		be.CdnPolicy.DefaultTtl = 0
	} else if cdnConfig.DefaultTtl != nil {
		be.CdnPolicy.DefaultTtl = *cdnConfig.DefaultTtl
		if be.CdnPolicy.DefaultTtl == 0 {
			be.CdnPolicy.ForceSendFields = append(be.CdnPolicy.ForceSendFields, "DefaultTtl")
		}
	} else {
		be.CdnPolicy.DefaultTtl = defaultCdnPolicy.DefaultTtl
	}

	if cdnConfig.NegativeCaching != nil {
		be.CdnPolicy.NegativeCaching = *cdnConfig.NegativeCaching
	} else {
		be.CdnPolicy.NegativeCaching = defaultCdnPolicy.NegativeCaching
	}

	negativeCachingPolicy := []*composite.BackendServiceCdnPolicyNegativeCachingPolicy{}
	if be.CdnPolicy.NegativeCaching {
		for _, policyRef := range cdnConfig.NegativeCachingPolicy {
			negativeCachingPolicy = append(
				negativeCachingPolicy,
				&composite.BackendServiceCdnPolicyNegativeCachingPolicy{
					Code: policyRef.Code,
					Ttl:  policyRef.Ttl,
				},
			)
		}
	}

	if len(negativeCachingPolicy) > 0 {
		be.CdnPolicy.NegativeCachingPolicy = negativeCachingPolicy
	}

	if cdnConfig.SignedUrlCacheMaxAgeSec != nil {
		be.CdnPolicy.SignedUrlCacheMaxAgeSec = *cdnConfig.SignedUrlCacheMaxAgeSec
	} else {
		be.CdnPolicy.SignedUrlCacheMaxAgeSec = defaultCdnPolicy.SignedUrlCacheMaxAgeSec
	}

	bypassCacheOnRequestHeaders := []*composite.BackendServiceCdnPolicyBypassCacheOnRequestHeader{}
	for _, policyRef := range cdnConfig.BypassCacheOnRequestHeaders {
		bypassCacheOnRequestHeaders = append(
			bypassCacheOnRequestHeaders,
			&composite.BackendServiceCdnPolicyBypassCacheOnRequestHeader{
				HeaderName: policyRef.HeaderName,
			},
		)
	}
	if len(bypassCacheOnRequestHeaders) > 0 {
		be.CdnPolicy.BypassCacheOnRequestHeaders = bypassCacheOnRequestHeaders
	}

	return be
}