Skip to content

Commit a1610eb

Browse files
committed
Enable health checks on all Kubernetes clusters by default
A per-resource health check config approach is implemented for enabling ease of adoption of new health checks, while avoiding migration of the backend database. Changes: - Added `default_kube` health check config which enables health checks on all Kubernetes clusters - Revised initialization and insert logic for health check configs Part of #58413
1 parent dcb8fff commit a1610eb

File tree

4 files changed

+88
-19
lines changed

4 files changed

+88
-19
lines changed

constants.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -775,9 +775,16 @@ const (
775775
var PresetRoles = []string{PresetEditorRoleName, PresetAccessRoleName, PresetAuditorRoleName}
776776

777777
const (
778-
// PresetDefaultHealthCheckConfigName is the name of a preset
779-
// default health_check_config that enables health checks for all resources.
780-
PresetDefaultHealthCheckConfigName = "default"
778+
// PresetDefaultHealthCheckConfigDBName is the name of a preset
779+
// health_check_config that enables health checks for all
780+
// database resources. For historical reasons, this preset is named
781+
// "default" even though it applies only to databases.
782+
PresetDefaultHealthCheckConfigDBName = "default"
783+
784+
// PresetDefaultHealthCheckConfigKubeName is the name of a preset
785+
// health_check_config that enables health checks for all
786+
// Kubernetes resources.
787+
PresetDefaultHealthCheckConfigKubeName = "default_kube"
781788
)
782789

783790
const (

lib/auth/init.go

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,21 +1489,57 @@ func createPresetDatabaseObjectImportRule(ctx context.Context, rules services.Da
14891489
// createPresetHealthCheckConfig creates a default preset health check config
14901490
// resource that enables health checks on all resources.
14911491
func createPresetHealthCheckConfig(ctx context.Context, svc services.HealthCheckConfig) error {
1492+
// To support developing health checks for multiple resources over time,
1493+
// while avoiding migration of the backend database,
1494+
// and enabling ease of health check adoption:
1495+
// - Create a health check preset for each resource (db, kube, etc)
1496+
14921497
page, _, err := svc.ListHealthCheckConfigs(ctx, 0, "")
14931498
if err != nil {
14941499
return trace.Wrap(err, "failed listing available health check configs")
14951500
}
1496-
if len(page) > 0 {
1501+
if len(page) == 0 {
1502+
// No health check configs exist.
1503+
// Create all preset configs.
1504+
presetDB := services.NewPresetHealthCheckConfigDB()
1505+
_, err = svc.CreateHealthCheckConfig(ctx, presetDB)
1506+
if err != nil && !trace.IsAlreadyExists(err) {
1507+
return trace.Wrap(err,
1508+
"failed creating preset health_check_config %s",
1509+
presetDB.GetMetadata().GetName(),
1510+
)
1511+
}
1512+
presetKube := services.NewPresetHealthCheckConfigKube()
1513+
_, err = svc.CreateHealthCheckConfig(ctx, presetKube)
1514+
if err != nil && !trace.IsAlreadyExists(err) {
1515+
return trace.Wrap(err,
1516+
"failed creating preset health_check_config %s",
1517+
presetKube.GetMetadata().GetName(),
1518+
)
1519+
}
14971520
return nil
1521+
} else {
1522+
// Health check configs exist.
1523+
// Create per-resource presets.
1524+
// Skip creating a DB preset; historically, it's the first, and already exists.
1525+
1526+
// Look for an existing kube preset.
1527+
for _, cfg := range page {
1528+
if cfg.GetMetadata().GetName() == teleport.PresetDefaultHealthCheckConfigKubeName {
1529+
return nil
1530+
}
1531+
}
1532+
// Create a kube preset.
1533+
presetKube := services.NewPresetHealthCheckConfigKube()
1534+
_, err = svc.CreateHealthCheckConfig(ctx, presetKube)
1535+
if err != nil && !trace.IsAlreadyExists(err) {
1536+
return trace.Wrap(err,
1537+
"failed creating preset health_check_config %s",
1538+
presetKube.GetMetadata().GetName(),
1539+
)
1540+
}
14981541
}
1499-
preset := services.NewPresetHealthCheckConfig()
1500-
_, err = svc.CreateHealthCheckConfig(ctx, preset)
1501-
if err != nil && !trace.IsAlreadyExists(err) {
1502-
return trace.Wrap(err,
1503-
"failed creating preset health_check_config %s",
1504-
preset.GetMetadata().GetName(),
1505-
)
1506-
}
1542+
15071543
return nil
15081544
}
15091545

lib/auth/init_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ func TestPresets(t *testing.T) {
985985
require.NoError(t, err)
986986
}
987987

988-
cfg, err := as.GetHealthCheckConfig(ctx, teleport.PresetDefaultHealthCheckConfigName)
988+
cfg, err := as.GetHealthCheckConfig(ctx, teleport.PresetDefaultHealthCheckConfigDBName)
989989
require.NoError(t, err)
990990
require.NotNil(t, cfg)
991991
})
@@ -1021,7 +1021,7 @@ func TestPresets(t *testing.T) {
10211021
as.SetClock(clock)
10221022

10231023
// an existing health check config should not be modified by init
1024-
cfg := services.NewPresetHealthCheckConfig()
1024+
cfg := services.NewPresetHealthCheckConfigDB()
10251025
cfg.Spec.Interval = durationpb.New(42 * time.Second)
10261026
cfg, err := as.CreateHealthCheckConfig(ctx, cfg)
10271027
require.NoError(t, err)

lib/services/presets.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -855,15 +855,15 @@ func NewPresetMCPUserRole() types.Role {
855855
return role
856856
}
857857

858-
// NewPresetHealthCheckConfig returns a preset default health_check_config that
859-
// enables health checks for all resources.
860-
func NewPresetHealthCheckConfig() *healthcheckconfigv1.HealthCheckConfig {
858+
// NewPresetHealthCheckConfigDB returns a preset health_check_config
859+
// that enables health checks for all databases resources.
860+
func NewPresetHealthCheckConfigDB() *healthcheckconfigv1.HealthCheckConfig {
861861
return &healthcheckconfigv1.HealthCheckConfig{
862862
Kind: types.KindHealthCheckConfig,
863863
Version: types.V1,
864864
Metadata: &headerv1.Metadata{
865-
Name: teleport.PresetDefaultHealthCheckConfigName,
866-
Description: "Enables all health checks by default",
865+
Name: teleport.PresetDefaultHealthCheckConfigDBName,
866+
Description: "Enables health checks for all databases by default",
867867
Namespace: apidefaults.Namespace,
868868
Labels: map[string]string{
869869
types.TeleportInternalResourceType: types.PresetResource,
@@ -881,6 +881,32 @@ func NewPresetHealthCheckConfig() *healthcheckconfigv1.HealthCheckConfig {
881881
}
882882
}
883883

884+
// NewPresetHealthCheckConfigKube returns a preset health_check_config
885+
// that enables health checks for all Kubernetes resources.
886+
func NewPresetHealthCheckConfigKube() *healthcheckconfigv1.HealthCheckConfig {
887+
return &healthcheckconfigv1.HealthCheckConfig{
888+
Kind: types.KindHealthCheckConfig,
889+
Version: types.V1,
890+
Metadata: &headerv1.Metadata{
891+
Name: teleport.PresetDefaultHealthCheckConfigKubeName,
892+
Description: "Enables health checks for all Kubernetes clusters by default",
893+
Namespace: apidefaults.Namespace,
894+
Labels: map[string]string{
895+
types.TeleportInternalResourceType: types.PresetResource,
896+
},
897+
},
898+
Spec: &healthcheckconfigv1.HealthCheckConfigSpec{
899+
Match: &healthcheckconfigv1.Matcher{
900+
// match all kubernetes clusters
901+
KubernetesLabels: []*labelv1.Label{{
902+
Name: types.Wildcard,
903+
Values: []string{types.Wildcard},
904+
}},
905+
},
906+
},
907+
}
908+
}
909+
884910
// bootstrapRoleMetadataLabels are metadata labels that will be applied to each role.
885911
// These are intended to add labels for older roles that didn't previously have them.
886912
func bootstrapRoleMetadataLabels() map[string]map[string]string {

0 commit comments

Comments
 (0)