Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve telepresence list performance #3712

Merged
merged 6 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions charts/telepresence/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ spec:
{{- end }}
{{- end }}
{{- end }}
{{- with .compatibility }}
{{- if .version }}
- name: COMPATIBILITY_VERSION
value: {{ .version }}
{{- end }}
{{- end }}
{{- if and .trafficManager .trafficManager.envTemplate }}
{{- template "traffic-manager-env" . }}
{{- end }}
Expand Down
9 changes: 9 additions & 0 deletions charts/telepresence/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,12 @@ client:
workloads:
argoRollouts:
enabled: false

# Use for testing only.
compatibility:
# Controls the enablement of features more recent than the given version. Only applicable
# for versions 2.18.0 and up, and only recognized by versions 2.21.0 and up. In other words,
# you can make a 2.21.0 version behave as far back as a 2.18.0 version, but you cannot
# alter the behavior of versions earlier than 2.21.0.
#
# version: 2.19.0
41 changes: 30 additions & 11 deletions cmd/traffic/cmd/manager/managerutil/envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/blang/semver/v4"
"github.com/go-json-experiment/json"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -70,6 +71,9 @@ type Env struct {
ClientConnectionTTL time.Duration `env:"CLIENT_CONNECTION_TTL, parser=time.ParseDuration"`

ArgoRolloutsEnabled bool `env:"ARGO_ROLLOUTS_ENABLED, parser=bool, default=false"`

// For testing only
CompatibilityVersion *semver.Version `env:"COMPATIBILITY_VERSION, parser=version, default="`
}

func (e *Env) GeneratorConfig(qualifiedAgentImage string) (agentmap.GeneratorConfig, error) {
Expand Down Expand Up @@ -116,39 +120,39 @@ func fieldTypeHandlers() map[reflect.Type]envconfig.FieldTypeHandler {
return uint16(pn), err
},
},
Setter: func(dst reflect.Value, src interface{}) { dst.SetUint(uint64(src.(uint16))) },
Setter: func(dst reflect.Value, src any) { dst.SetUint(uint64(src.(uint16))) },
}
fhs[reflect.TypeOf(k8sapi.AppProtocolStrategy(0))] = envconfig.FieldTypeHandler{
Parsers: map[string]func(string) (any, error){
"app-proto-strategy": func(str string) (any, error) {
return k8sapi.NewAppProtocolStrategy(str)
},
},
Setter: func(dst reflect.Value, src interface{}) { dst.SetInt(int64(src.(k8sapi.AppProtocolStrategy))) },
Setter: func(dst reflect.Value, src any) { dst.SetInt(int64(src.(k8sapi.AppProtocolStrategy))) },
}
fhs[reflect.TypeOf(agentconfig.InjectPolicy(0))] = envconfig.FieldTypeHandler{
Parsers: map[string]func(string) (any, error){
"enable-policy": func(str string) (any, error) {
return agentconfig.NewEnablePolicy(str)
},
},
Setter: func(dst reflect.Value, src interface{}) { dst.SetInt(int64(src.(agentconfig.InjectPolicy))) },
Setter: func(dst reflect.Value, src any) { dst.SetInt(int64(src.(agentconfig.InjectPolicy))) },
}
fhs[reflect.TypeOf(resource.Quantity{})] = envconfig.FieldTypeHandler{
Parsers: map[string]func(string) (any, error){
"quantity": func(str string) (any, error) {
return resource.ParseQuantity(str)
},
},
Setter: func(dst reflect.Value, src interface{}) { dst.Set(reflect.ValueOf(src.(resource.Quantity))) },
Setter: func(dst reflect.Value, src any) { dst.Set(reflect.ValueOf(src.(resource.Quantity))) },
}
fhs[reflect.TypeOf(netip.Addr{})] = envconfig.FieldTypeHandler{
Parsers: map[string]func(string) (any, error){
"ip": func(str string) (any, error) { //nolint:unparam // API requirement
return netip.ParseAddr(str)
},
},
Setter: func(dst reflect.Value, src interface{}) { dst.Set(reflect.ValueOf(src.(netip.Addr))) },
Setter: func(dst reflect.Value, src any) { dst.Set(reflect.ValueOf(src.(netip.Addr))) },
}
fhs[reflect.TypeOf([]string{})] = envconfig.FieldTypeHandler{
Parsers: map[string]func(string) (any, error){
Expand All @@ -163,7 +167,7 @@ func fieldTypeHandlers() map[reflect.Type]envconfig.FieldTypeHandler {
return ss, nil
},
},
Setter: func(dst reflect.Value, src interface{}) { dst.Set(reflect.ValueOf(src.([]string))) },
Setter: func(dst reflect.Value, src any) { dst.Set(reflect.ValueOf(src.([]string))) },
}
fhs[reflect.TypeOf([]netip.Prefix{})] = envconfig.FieldTypeHandler{
Parsers: map[string]func(string) (any, error){
Expand All @@ -182,7 +186,7 @@ func fieldTypeHandlers() map[reflect.Type]envconfig.FieldTypeHandler {
return ns, nil
},
},
Setter: func(dst reflect.Value, src interface{}) { dst.Set(reflect.ValueOf(src.([]netip.Prefix))) },
Setter: func(dst reflect.Value, src any) { dst.Set(reflect.ValueOf(src.([]netip.Prefix))) },
}
fhs[reflect.TypeOf([]core.LocalObjectReference{})] = envconfig.FieldTypeHandler{
Parsers: map[string]func(string) (any, error){
Expand All @@ -197,7 +201,7 @@ func fieldTypeHandlers() map[reflect.Type]envconfig.FieldTypeHandler {
return rr, nil
},
},
Setter: func(dst reflect.Value, src interface{}) { dst.Set(reflect.ValueOf(src.([]core.LocalObjectReference))) },
Setter: func(dst reflect.Value, src any) { dst.Set(reflect.ValueOf(src.([]core.LocalObjectReference))) },
}
fhs[reflect.TypeOf(&core.ResourceRequirements{})] = envconfig.FieldTypeHandler{
Parsers: map[string]func(string) (any, error){
Expand All @@ -212,7 +216,7 @@ func fieldTypeHandlers() map[reflect.Type]envconfig.FieldTypeHandler {
return rr, nil
},
},
Setter: func(dst reflect.Value, src interface{}) { dst.Set(reflect.ValueOf(src.(*core.ResourceRequirements))) },
Setter: func(dst reflect.Value, src any) { dst.Set(reflect.ValueOf(src.(*core.ResourceRequirements))) },
}
fhs[reflect.TypeOf(&core.SecurityContext{})] = envconfig.FieldTypeHandler{
Parsers: map[string]func(string) (any, error){
Expand All @@ -227,15 +231,30 @@ func fieldTypeHandlers() map[reflect.Type]envconfig.FieldTypeHandler {
return rr, nil
},
},
Setter: func(dst reflect.Value, src interface{}) { dst.Set(reflect.ValueOf(src.(*core.SecurityContext))) },
Setter: func(dst reflect.Value, src any) { dst.Set(reflect.ValueOf(src.(*core.SecurityContext))) },
}
fhs[reflect.TypeOf(true)] = envconfig.FieldTypeHandler{
Parsers: map[string]func(string) (any, error){
"bool": func(str string) (any, error) {
return strconv.ParseBool(str)
},
},
Setter: func(dst reflect.Value, src interface{}) { dst.SetBool(src.(bool)) },
Setter: func(dst reflect.Value, src any) { dst.SetBool(src.(bool)) },
}
fhs[reflect.TypeOf(&semver.Version{})] = envconfig.FieldTypeHandler{
Parsers: map[string]func(string) (any, error){
"version": func(str string) (any, error) {
if str == "" {
return nil, nil
}
v, err := semver.Parse(str)
if err != nil {
return nil, err
}
return &v, nil
},
},
Setter: func(dst reflect.Value, src any) { dst.Set(reflect.ValueOf(src.(*semver.Version))) },
}
return fhs
}
Expand Down
12 changes: 7 additions & 5 deletions cmd/traffic/cmd/manager/mutator/constants.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package mutator

import "github.com/telepresenceio/telepresence/v2/pkg/agentconfig"
import (
"github.com/telepresenceio/telepresence/v2/pkg/agentconfig"
"github.com/telepresenceio/telepresence/v2/pkg/workload"
)

const (
DomainPrefix = "telepresence.getambassador.io/"
InjectAnnotation = DomainPrefix + "inject-" + agentconfig.ContainerName
ServiceNameAnnotation = DomainPrefix + "inject-service-name"
ManualInjectAnnotation = DomainPrefix + "manually-injected"
InjectAnnotation = workload.DomainPrefix + "inject-" + agentconfig.ContainerName
ServiceNameAnnotation = workload.DomainPrefix + "inject-service-name"
ManualInjectAnnotation = workload.DomainPrefix + "manually-injected"
)
23 changes: 15 additions & 8 deletions cmd/traffic/cmd/manager/mutator/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/telepresenceio/telepresence/v2/pkg/agentmap"
"github.com/telepresenceio/telepresence/v2/pkg/informer"
"github.com/telepresenceio/telepresence/v2/pkg/tracing"
"github.com/telepresenceio/telepresence/v2/pkg/workload"
)

type Map interface {
Expand Down Expand Up @@ -163,6 +164,14 @@ func (c *configWatcher) isRolloutNeeded(ctx context.Context, wl k8sapi.Workload,
}
return true
}
if ac == nil {
if okPods < runningPods {
dlog.Debugf(ctx, "Rollout of %s.%s is necessary. At least one pod still has an agent",
wl.GetName(), wl.GetNamespace())
return true
}
return false
}
dlog.Debugf(ctx, "Rollout of %s.%s is not necessary. At least one pod have the desired agent state",
wl.GetName(), wl.GetNamespace())
return false
Expand Down Expand Up @@ -229,8 +238,6 @@ func isRolloutNeededForPod(ctx context.Context, ac *agentconfig.Sidecar, name, n
return ""
}

const AnnRestartedAt = DomainPrefix + "restartedAt"

func (c *configWatcher) triggerRollout(ctx context.Context, wl k8sapi.Workload, ac *agentconfig.Sidecar) {
lck := c.getRolloutLock(wl)
if !lck.TryLock() {
Expand Down Expand Up @@ -269,10 +276,10 @@ func generateRestartAnnotationPatch(podTemplate *core.PodTemplateSpec) string {
basePointer := "/spec/template/metadata/annotations"
pointer := fmt.Sprintf(
basePointer+"/%s",
strings.ReplaceAll(AnnRestartedAt, "/", "~1"),
strings.ReplaceAll(workload.AnnRestartedAt, "/", "~1"),
)

if _, ok := podTemplate.Annotations[AnnRestartedAt]; ok {
if _, ok := podTemplate.Annotations[workload.AnnRestartedAt]; ok {
return fmt.Sprintf(
`[{"op": "replace", "path": "%s", "value": "%s"}]`, pointer, time.Now().Format(time.RFC3339),
)
Expand Down Expand Up @@ -833,9 +840,9 @@ func (c *configWatcher) Start(ctx context.Context) {
for i, ns := range nss {
c.cms[i] = c.startConfigMap(ctx, ns)
c.svs[i] = c.startServices(ctx, ns)
c.dps[i] = c.startDeployments(ctx, ns)
c.rss[i] = c.startReplicaSets(ctx, ns)
c.sss[i] = c.startStatefulSets(ctx, ns)
c.dps[i] = workload.StartDeployments(ctx, ns)
c.rss[i] = workload.StartReplicaSets(ctx, ns)
c.sss[i] = workload.StartStatefulSets(ctx, ns)
c.startPods(ctx, ns)
kf := informer.GetK8sFactory(ctx, ns)
kf.Start(ctx.Done())
Expand All @@ -844,7 +851,7 @@ func (c *configWatcher) Start(ctx context.Context) {
if managerutil.ArgoRolloutsEnabled(ctx) {
c.rls = make([]cache.SharedIndexInformer, len(nss))
for i, ns := range nss {
c.rls[i] = c.startRollouts(ctx, ns)
c.rls[i] = workload.StartRollouts(ctx, ns)
rf := informer.GetArgoRolloutsFactory(ctx, ns)
rf.Start(ctx.Done())
rf.WaitForCacheSync(ctx.Done())
Expand Down
87 changes: 0 additions & 87 deletions cmd/traffic/cmd/manager/mutator/workload_state.go

This file was deleted.

Loading
Loading