Skip to content

Commit dd79107

Browse files
committed
Implement immediate schedule support
1 parent 85ca459 commit dd79107

File tree

6 files changed

+176
-44
lines changed

6 files changed

+176
-44
lines changed

api/client/webclient/webclient.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,12 @@ type AutoUpdateSettings struct {
336336
ToolsVersion string `json:"tools_version"`
337337
// ToolsMode defines mode client auto update feature `enabled|disabled`.
338338
ToolsMode string `json:"tools_mode"`
339+
// AgentVersion defines the version of teleport that agents enrolled into autoupdates should run.
340+
AgentVersion string `json:"agent_version"`
341+
// AgentAutoUpdate indicates if the requesting agent should attempt to update now.
342+
AgentAutoUpdate bool `json:"agent_auto_update"`
343+
// AgentUpdateJitterSeconds defines the jitter time an agent should wait before updating.
344+
AgentUpdateJitterSeconds int `json:"agent_update_jitter_seconds"`
339345
}
340346

341347
// KubeProxySettings is kubernetes proxy settings

api/types/autoupdate/rollout_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestNewAutoUpdateAgentRollout(t *testing.T) {
4141
spec: &autoupdate.AutoUpdateAgentRolloutSpec{
4242
StartVersion: "1.2.3",
4343
TargetVersion: "2.3.4-dev",
44-
Schedule: AgentsScheduleRegular,
44+
Schedule: AgentsScheduleImmediate,
4545
AutoupdateMode: AgentsUpdateModeEnabled,
4646
Strategy: AgentsStrategyHaltOnError,
4747
},
@@ -57,7 +57,7 @@ func TestNewAutoUpdateAgentRollout(t *testing.T) {
5757
Spec: &autoupdate.AutoUpdateAgentRolloutSpec{
5858
StartVersion: "1.2.3",
5959
TargetVersion: "2.3.4-dev",
60-
Schedule: AgentsScheduleRegular,
60+
Schedule: AgentsScheduleImmediate,
6161
AutoupdateMode: AgentsUpdateModeEnabled,
6262
Strategy: AgentsStrategyHaltOnError,
6363
},
@@ -74,7 +74,7 @@ func TestNewAutoUpdateAgentRollout(t *testing.T) {
7474
name: "missing start version",
7575
spec: &autoupdate.AutoUpdateAgentRolloutSpec{
7676
TargetVersion: "2.3.4-dev",
77-
Schedule: AgentsScheduleRegular,
77+
Schedule: AgentsScheduleImmediate,
7878
AutoupdateMode: AgentsUpdateModeEnabled,
7979
Strategy: AgentsStrategyHaltOnError,
8080
},
@@ -87,7 +87,7 @@ func TestNewAutoUpdateAgentRollout(t *testing.T) {
8787
spec: &autoupdate.AutoUpdateAgentRolloutSpec{
8888
StartVersion: "1.2.3",
8989
TargetVersion: "2-3-4",
90-
Schedule: AgentsScheduleRegular,
90+
Schedule: AgentsScheduleImmediate,
9191
AutoupdateMode: AgentsUpdateModeEnabled,
9292
Strategy: AgentsStrategyHaltOnError,
9393
},
@@ -100,7 +100,7 @@ func TestNewAutoUpdateAgentRollout(t *testing.T) {
100100
spec: &autoupdate.AutoUpdateAgentRolloutSpec{
101101
StartVersion: "1.2.3",
102102
TargetVersion: "2.3.4-dev",
103-
Schedule: AgentsScheduleRegular,
103+
Schedule: AgentsScheduleImmediate,
104104
AutoupdateMode: "invalid-mode",
105105
Strategy: AgentsStrategyHaltOnError,
106106
},
@@ -126,7 +126,7 @@ func TestNewAutoUpdateAgentRollout(t *testing.T) {
126126
spec: &autoupdate.AutoUpdateAgentRolloutSpec{
127127
StartVersion: "1.2.3",
128128
TargetVersion: "2.3.4-dev",
129-
Schedule: AgentsScheduleRegular,
129+
Schedule: AgentsScheduleImmediate,
130130
AutoupdateMode: AgentsUpdateModeEnabled,
131131
Strategy: "invalid-strategy",
132132
},

api/types/autoupdate/utils.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ func checkToolsMode(mode string) error {
5151

5252
func checkScheduleName(schedule string) error {
5353
switch schedule {
54-
case AgentsScheduleRegular, AgentsScheduleImmediate:
54+
case AgentsScheduleImmediate:
5555
return nil
56+
case AgentsScheduleRegular:
57+
return trace.BadParameter("regular schedule is not implemented yet")
5658
default:
5759
return trace.BadParameter("unsupported schedule type: %q", schedule)
5860
}

api/types/autoupdate/version_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestNewAutoUpdateVersion(t *testing.T) {
9494
Agents: &autoupdate.AutoUpdateVersionSpecAgents{
9595
StartVersion: "1.2.3-dev.1",
9696
TargetVersion: "1.2.3-dev.2",
97-
Schedule: AgentsScheduleRegular,
97+
Schedule: AgentsScheduleImmediate,
9898
Mode: AgentsUpdateModeEnabled,
9999
},
100100
},
@@ -111,7 +111,7 @@ func TestNewAutoUpdateVersion(t *testing.T) {
111111
Agents: &autoupdate.AutoUpdateVersionSpecAgents{
112112
StartVersion: "1.2.3-dev.1",
113113
TargetVersion: "1.2.3-dev.2",
114-
Schedule: AgentsScheduleRegular,
114+
Schedule: AgentsScheduleImmediate,
115115
Mode: AgentsUpdateModeEnabled,
116116
},
117117
},
@@ -124,7 +124,7 @@ func TestNewAutoUpdateVersion(t *testing.T) {
124124
StartVersion: "",
125125
TargetVersion: "1.2.3",
126126
Mode: AgentsUpdateModeEnabled,
127-
Schedule: AgentsScheduleRegular,
127+
Schedule: AgentsScheduleImmediate,
128128
},
129129
},
130130
assertErr: func(t *testing.T, err error, a ...any) {
@@ -138,7 +138,7 @@ func TestNewAutoUpdateVersion(t *testing.T) {
138138
StartVersion: "1.2.3-dev",
139139
TargetVersion: "",
140140
Mode: AgentsUpdateModeEnabled,
141-
Schedule: AgentsScheduleRegular,
141+
Schedule: AgentsScheduleImmediate,
142142
},
143143
},
144144
assertErr: func(t *testing.T, err error, a ...any) {
@@ -152,7 +152,7 @@ func TestNewAutoUpdateVersion(t *testing.T) {
152152
StartVersion: "17-0-0",
153153
TargetVersion: "1.2.3",
154154
Mode: AgentsUpdateModeEnabled,
155-
Schedule: AgentsScheduleRegular,
155+
Schedule: AgentsScheduleImmediate,
156156
},
157157
},
158158
assertErr: func(t *testing.T, err error, a ...any) {
@@ -166,7 +166,7 @@ func TestNewAutoUpdateVersion(t *testing.T) {
166166
StartVersion: "1.2.3",
167167
TargetVersion: "17-0-0",
168168
Mode: AgentsUpdateModeEnabled,
169-
Schedule: AgentsScheduleRegular,
169+
Schedule: AgentsScheduleImmediate,
170170
},
171171
},
172172
assertErr: func(t *testing.T, err error, a ...any) {

lib/web/apiserver.go

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import (
6565
"github.com/gravitational/teleport/api/client/webclient"
6666
"github.com/gravitational/teleport/api/constants"
6767
apidefaults "github.com/gravitational/teleport/api/defaults"
68+
autoupdatepb "github.com/gravitational/teleport/api/gen/proto/go/teleport/autoupdate/v1"
6869
mfav1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/mfa/v1"
6970
notificationsv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/notifications/v1"
7071
"github.com/gravitational/teleport/api/mfa"
@@ -130,6 +131,8 @@ const (
130131
// DefaultFeatureWatchInterval is the default time in which the feature watcher
131132
// should ping the auth server to check for updated features
132133
DefaultFeatureWatchInterval = time.Minute * 5
134+
// DefaultAgentUpdateJitterSeconds is the default jitter agents should wait before updating.
135+
DefaultAgentUpdateJitterSeconds = 60
133136
)
134137

135138
// healthCheckAppServerFunc defines a function used to perform a health check
@@ -1547,27 +1550,19 @@ func (h *Handler) find(w http.ResponseWriter, r *http.Request, p httprouter.Para
15471550
if err != nil && !trace.IsNotFound(err) && !trace.IsNotImplemented(err) {
15481551
h.logger.ErrorContext(r.Context(), "failed to receive AutoUpdateConfig", "error", err)
15491552
}
1550-
// If we can't get the AU config or tools AU are not configured, we default to "disabled".
1551-
// This ensures we fail open and don't accidentally update agents if something is going wrong.
1552-
// If we want to enable AUs by default, it would be better to create a default "autoupdate_config" resource
1553-
// than changing this logic.
1554-
if autoUpdateConfig.GetSpec().GetTools() == nil {
1555-
response.AutoUpdate.ToolsMode = autoupdate.ToolsUpdateModeDisabled
1556-
} else {
1557-
response.AutoUpdate.ToolsMode = autoUpdateConfig.GetSpec().GetTools().GetMode()
1558-
}
15591553

15601554
autoUpdateVersion, err := h.cfg.AccessPoint.GetAutoUpdateVersion(r.Context())
15611555
// TODO(vapopov) DELETE IN v18.0.0 check of IsNotImplemented, must be backported to all latest supported versions.
15621556
if err != nil && !trace.IsNotFound(err) && !trace.IsNotImplemented(err) {
15631557
h.logger.ErrorContext(r.Context(), "failed to receive AutoUpdateVersion", "error", err)
15641558
}
1565-
// If we can't get the AU version or tools AU version is not specified, we default to the current proxy version.
1566-
// This ensures we always advertise a version compatible with the cluster.
1567-
if autoUpdateVersion.GetSpec().GetTools() == nil {
1568-
response.AutoUpdate.ToolsVersion = api.Version
1569-
} else {
1570-
response.AutoUpdate.ToolsVersion = autoUpdateVersion.GetSpec().GetTools().GetTargetVersion()
1559+
1560+
response.AutoUpdate = webclient.AutoUpdateSettings{
1561+
ToolsMode: getToolsMode(autoUpdateConfig),
1562+
ToolsVersion: getToolsVersion(autoUpdateVersion),
1563+
AgentUpdateJitterSeconds: DefaultAgentUpdateJitterSeconds,
1564+
AgentVersion: getAgentVersion(autoUpdateVersion),
1565+
AgentAutoUpdate: agentShouldUpdate(autoUpdateConfig, autoUpdateVersion),
15711566
}
15721567

15731568
return response, nil
@@ -5122,3 +5117,64 @@ func readEtagFromAppHash(fs http.FileSystem) (string, error) {
51225117

51235118
return etag, nil
51245119
}
5120+
5121+
func getToolsMode(config *autoupdatepb.AutoUpdateConfig) string {
5122+
// If we can't get the AU config or if AUs are not configured, we default to "disabled".
5123+
// This ensures we fail open and don't accidentally update agents if something is going wrong.
5124+
// If we want to enable AUs by default, it would be better to create a default "autoupdate_config" resource
5125+
// than changing this logic.
5126+
if config.GetSpec().GetTools() == nil {
5127+
return autoupdate.ToolsUpdateModeDisabled
5128+
}
5129+
return config.GetSpec().GetTools().GetMode()
5130+
}
5131+
5132+
func getToolsVersion(version *autoupdatepb.AutoUpdateVersion) string {
5133+
// If we can't get the AU version or tools AU version is not specified, we default to the current proxy version.
5134+
// This ensures we always advertise a version compatible with the cluster.
5135+
if version.GetSpec().GetTools() == nil {
5136+
return api.Version
5137+
}
5138+
return version.GetSpec().GetTools().GetTargetVersion()
5139+
}
5140+
5141+
func getAgentVersion(version *autoupdatepb.AutoUpdateVersion) string {
5142+
// If we can't get the AU version or tools AU version is not specified, we default to the current proxy version.
5143+
// This ensures we always advertise a version compatible with the cluster.
5144+
// TODO: read the version from the autoupdate_agent_rollout when the resource is implemented
5145+
if version.GetSpec().GetAgents() == nil {
5146+
return api.Version
5147+
}
5148+
5149+
return version.GetSpec().GetAgents().GetTargetVersion()
5150+
}
5151+
5152+
func agentShouldUpdate(config *autoupdatepb.AutoUpdateConfig, version *autoupdatepb.AutoUpdateVersion) bool {
5153+
// TODO: read the data from the autoupdate_agent_rollout when the resource is implemented
5154+
5155+
// If we can't get the AU config or if AUs are not configured, we default to "disabled".
5156+
// This ensures we fail open and don't accidentally update agents if something is going wrong.
5157+
// If we want to enable AUs by default, it would be better to create a default "autoupdate_config" resource
5158+
// than changing this logic.
5159+
if config.GetSpec().GetAgents() == nil {
5160+
return false
5161+
}
5162+
if version.GetSpec().GetAgents() == nil {
5163+
return false
5164+
}
5165+
configMode := config.GetSpec().GetAgents().GetMode()
5166+
versionMode := version.GetSpec().GetAgents().GetMode()
5167+
5168+
// We update only if both version and config agent modes are "enabled"
5169+
if configMode != autoupdate.AgentsUpdateModeEnabled || versionMode != autoupdate.AgentsUpdateModeEnabled {
5170+
return false
5171+
}
5172+
5173+
scheduleName := version.GetSpec().GetAgents().GetSchedule()
5174+
if scheduleName == autoupdate.AgentsScheduleImmediate {
5175+
return true
5176+
}
5177+
5178+
// TODO: add support for the regular schedule name as we implement groups and autoupdate_agent_rollout
5179+
return false
5180+
}

0 commit comments

Comments
 (0)