Skip to content

Commit 857208b

Browse files
committed
add flag to disable root endpoint
1 parent 44f9ce5 commit 857208b

6 files changed

Lines changed: 43 additions & 23 deletions

File tree

.github/workflows/build_and_push.yaml

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,11 @@ jobs:
1919
run: make build
2020
# - name: Lint Dockerfiles
2121
# run: make docker-lint
22-
23-
# TODO remove this after quay creds are set
24-
- name: Build all Docker Images
25-
run: make docker-build-all
26-
27-
# - name: Login to Quay
28-
# uses: docker/login-action@v3
29-
# with:
30-
# registry: quay.io
31-
# username: ${{ secrets.QUAY_USERNAME }}
32-
# password: ${{ secrets.QUAY_ROBOT_TOKEN }}
33-
34-
# - name: Push all Docker Images
35-
# run: make docker-push-all
22+
- name: Login to Quay
23+
uses: docker/login-action@v3
24+
with:
25+
registry: quay.io
26+
username: ${{ secrets.QUAY_USERNAME }}
27+
password: ${{ secrets.QUAY_ROBOT_TOKEN }}
28+
- name: Push all Docker Images
29+
run: make docker-push-all

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ The aim of this application is to be used in the context of trainings for Docker
1010

1111
<img width="500" alt="Screenshot 2025-06-04 at 15 40 30" src="https://github.com/user-attachments/assets/7cafc452-4f21-4202-8379-2a983ecbf122" />
1212

13-
1413
## Available Endpoints
1514

1615
> **_NOTE:_** The application offers the following endpoints on port **8080**
@@ -50,6 +49,8 @@ If everything is fine the application will respond with a 200 status code, if no
5049
| `leak cpu` | Leak CPU |
5150
| `request <url>` | Request a URL, e.g., `request https://www.kubermatic.com/` |
5251
| `delay / <seconds>` | Set delay for the root endpoint (`/`) in seconds, e.g., `delay / 5` |
52+
| `disable /` | The root endpoint (`/`) will respond with a 503 status code |
53+
| `enable /` | The root endpoint (`/`) will respond with a 200 status code |
5354

5455
> **_INSIDE A CONTAINER_** If you want to send commands to the application you have to use of `docker attach my-training-application-container`. The container als has to have `tty` enabled.
5556
@@ -65,8 +66,8 @@ spec:
6566
- name: training-application
6667
image: quay.io/kubermatic-labs/training-application:3.0.0
6768
imagePullPolicy: Always
68-
tty: true # <= add those flags
69-
stdin: true # <= add those flags
69+
tty: true # <= add this flag
70+
stdin: true # <= add this flag
7071
ports:
7172
- name: http
7273
containerPort: 8080
@@ -123,12 +124,19 @@ spec:
123124
- **Default Value**: "not set"
124125
- **Usage**: via config file or via the environment variable `APP_COLOR`
125126

127+
### `rootEnabled`
128+
129+
- **Description**: Flag to indicate if the root endpoint (`/`) is enabled or not
130+
- **Type**: bool
131+
- **Default Value**: true
132+
- **Usage**: configurable via the commands `enable /` and `disable /`
133+
126134
### `rootDelaySeconds`
127135

128136
- **Description**: For delaying requests to the root endpoint
129137
- **Type**: int
130138
- **Default Value**: 0
131-
- **Usage**: via config file or via the command `delay / <seconds>`, eg "delay / 10"
139+
- **Usage**: via the command `delay / <seconds>`, eg "delay / 10"
132140

133141
### `startUpDelaySeconds`
134142

src/cli.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ func (cli *cli) executeCommand(command string) error {
108108
return fmt.Errorf("error on converting delay string '%s' to int: %s", delayString, err)
109109
}
110110
log.Infof("Set delay for the root endpoint ('/') to '%d' seconds", cli.config.rootDelaySeconds)
111+
} else if strings.HasPrefix(command, "disable /") {
112+
cli.config.rootEnabled = false
113+
log.Info("Disabled the root endpoint ('/')")
114+
} else if strings.HasPrefix(command, "enable /") {
115+
cli.config.rootEnabled = true
116+
log.Info("Enabled the root endpoint ('/')")
111117
} else {
112118
return fmt.Errorf("unknown command '%s'", command)
113119
}

src/config.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type appConfig struct {
1616
configFilePath string
1717
alive bool
1818
ready bool
19+
rootEnabled bool
1920
rootDelaySeconds int
2021
startUpDelaySeconds int
2122
tearDownDelaySeconds int
@@ -32,6 +33,7 @@ func (appConfig *appConfig) logAppConfig() {
3233
log.Infof(" configFilePath: %v", appConfig.configFilePath)
3334
log.Infof(" ready: %v", appConfig.ready)
3435
log.Infof(" alive: %v", appConfig.alive)
36+
log.Infof(" / enabled: %v", appConfig.rootEnabled)
3537
log.Infof(" / delay seconds: %d", appConfig.rootDelaySeconds)
3638
log.Infof(" startup delay seconds: %d", appConfig.startUpDelaySeconds)
3739
log.Infof(" teardown delay seconds: %d", appConfig.tearDownDelaySeconds)
@@ -49,6 +51,7 @@ func newAppConfig(configFilePath string) *appConfig {
4951
configFilePath: configFilePath,
5052
alive: true,
5153
ready: false,
54+
rootEnabled: true,
5255
rootDelaySeconds: 0,
5356
startUpDelaySeconds: 0,
5457
tearDownDelaySeconds: 0,
@@ -72,7 +75,6 @@ func (appConfig *appConfig) initAppConfig(isReady bool) {
7275
appConfig.applicationMessage = getAppConfigStringValue(fileConfig, "message", "APP_MESSAGE", "not set")
7376
appConfig.color = getAppConfigStringValue(fileConfig, "color", "APP_COLOR", "not set")
7477
appConfig.logToFileOnly = getAppConfigBoolValue(fileConfig, "logToFileOnly", "", false)
75-
appConfig.rootDelaySeconds = getAppConfigIntValue(fileConfig, "rootDelaySeconds", "", 0)
7678
appConfig.startUpDelaySeconds = getAppConfigIntValue(fileConfig, "startUpDelaySeconds", "", 0)
7779
appConfig.tearDownDelaySeconds = getAppConfigIntValue(fileConfig, "tearDownDelaySeconds", "", 0)
7880
catMode := getAppConfigBoolValue(fileConfig, "catMode", "", false)
@@ -94,7 +96,7 @@ func getAppConfigStringValue(fileConfig *properties.Properties, fileConfigProper
9496
if fileConfig == nil {
9597
return defaultValue
9698
}
97-
return fileConfig.GetString(fileConfigProperty, "")
99+
return fileConfig.GetString(fileConfigProperty, defaultValue)
98100
}
99101

100102
func getAppConfigBoolValue(fileConfig *properties.Properties, fileConfigProperty, envVarName string, defaultValue bool) bool {
@@ -113,11 +115,14 @@ func getAppConfigBoolValue(fileConfig *properties.Properties, fileConfigProperty
113115
return defaultValue
114116
}
115117
fileConfigPropertyValue := fileConfig.GetString(fileConfigProperty, "")
118+
if fileConfigPropertyValue == "" {
119+
return defaultValue
120+
}
116121
value, err := strconv.ParseBool(fileConfigPropertyValue)
117122
if err != nil {
118123
log.Errorf("could not convert file configuration property named '%s' with value '%s' to bool:", fileConfigProperty, fileConfigPropertyValue)
119124
return defaultValue
120-
}
125+
}
121126
return value
122127
}
123128

@@ -137,6 +142,9 @@ func getAppConfigIntValue(fileConfig *properties.Properties, fileConfigProperty,
137142
return defaultValue
138143
}
139144
fileConfigPropertyValue := fileConfig.GetString(fileConfigProperty, "")
145+
if fileConfigPropertyValue == "" {
146+
return defaultValue
147+
}
140148
value, err := strconv.Atoi(fileConfigPropertyValue)
141149
if err != nil {
142150
log.Errorf("could not convert file configuration property named '%s' with value '%s' to int:", fileConfigProperty, fileConfigPropertyValue)

src/server.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ func (s *server) run() {
3838

3939
func (s *server) handleRoot(w http.ResponseWriter, r *http.Request) {
4040
log.Info("Request to root endpoint ('/')")
41+
if !s.config.rootEnabled {
42+
w.WriteHeader(http.StatusServiceUnavailable)
43+
log.Info("Root endpoint ('/') responded with Status Code 503 Service Unavailable due to root endpoint is disabled")
44+
} else {
4145
if s.config.rootDelaySeconds > 0 {
4246
for i := 0; i < s.config.rootDelaySeconds; i++ {
4347
log.Infof("Delayed Response for %d of %d seconds", i+1, s.config.rootDelaySeconds)
@@ -67,12 +71,13 @@ func (s *server) handleRoot(w http.ResponseWriter, r *http.Request) {
6771
fmt.Fprintf(w, "Hostname: %s<br>", hostName)
6872

6973
if s.config.catImageUrl != "" {
70-
fmt.Fprint(w, "<h2>The promised cute cat</h2>")
74+
fmt.Fprint(w, "<h2>The cute cat</h2>")
7175
fmt.Fprintf(w, "<img src='%s' width='500px'></img>", s.config.catImageUrl)
7276
}
7377

7478
fmt.Fprint(w, "</body></htlml>")
7579
}
80+
}
7681

7782
func (s *server) handleLiveness(w http.ResponseWriter, r *http.Request) {
7883
log.Info("Request to liveness endpoint ('/liveness')")

training-application.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ logToFileOnly = false
66
catMode = false
77
startUpDelaySeconds = 0
88
tearDownDelaySeconds = 0
9-
rootDelaySeconds = 0

0 commit comments

Comments
 (0)