Skip to content

Commit a6bfb92

Browse files
authored
Merge pull request kubernetes#124104 from SergeyKanzhelev/grpcTls
allow agnhost to set TLS gRPC
2 parents 01b2e12 + e9424f3 commit a6bfb92

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

test/images/agnhost/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ controlled with the time delay or via http control server.
266266
- `--port` (default: `5000`) can be used to override the gRPC port number.
267267
- `--http-port` (default: `8080`) can be used to override the http control server port number.
268268
- `--service` (default: ``) can be used used to specify which service this endpoint will respond to.
269+
- `--tls-cert-file` File containing an x509 certificate for gRPC TLS. (CA cert, if any, concatenated after server cert).
270+
- `--tls-private-key-file` File containing an x509 private key matching `--tls-cert-file`.
269271

270272
Usage:
271273

test/images/agnhost/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.48
1+
2.49

test/images/agnhost/grpc-health-checking/grpc-health-checking.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
"google.golang.org/grpc"
3232
"google.golang.org/grpc/codes"
33+
"google.golang.org/grpc/credentials"
3334
"google.golang.org/grpc/health/grpc_health_v1"
3435
"google.golang.org/grpc/status"
3536
)
@@ -49,13 +50,19 @@ var (
4950
delayUnhealthySec int
5051
service string
5152
forceUnhealthy *bool
53+
certFile string
54+
privKeyFile string
5255
)
5356

5457
func init() {
5558
CmdGrpcHealthChecking.Flags().IntVar(&port, "port", 5000, "Port number.")
5659
CmdGrpcHealthChecking.Flags().IntVar(&httpPort, "http-port", 8080, "Port number for the /make-serving and /make-not-serving.")
5760
CmdGrpcHealthChecking.Flags().IntVar(&delayUnhealthySec, "delay-unhealthy-sec", -1, "Number of seconds to delay before start reporting NOT_SERVING, negative value indicates never.")
5861
CmdGrpcHealthChecking.Flags().StringVar(&service, "service", "", "Service name to register the health check for.")
62+
CmdGrpcHealthChecking.Flags().StringVar(&certFile, "tls-cert-file", "",
63+
"File containing an x509 certificate for gRPC TLS. (CA cert, if any, concatenated after server cert).")
64+
CmdGrpcHealthChecking.Flags().StringVar(&privKeyFile, "tls-private-key-file", "",
65+
"File containing an x509 private key matching --tls-cert-file.")
5966
forceUnhealthy = nil
6067
}
6168

@@ -95,6 +102,13 @@ func NewHealthChecker(started time.Time) *HealthChecker {
95102
func main(cmd *cobra.Command, args []string) {
96103
started := time.Now()
97104

105+
// Validate flags
106+
//
107+
// if certFile or privKeyFile are not both set, exit with error
108+
if (certFile == "" && privKeyFile != "") || (certFile != "" && privKeyFile == "") {
109+
log.Fatalf("Both --tls-cert-file and --tls-private-key-file must be set")
110+
}
111+
98112
http.HandleFunc("/make-not-serving", func(w http.ResponseWriter, r *http.Request) {
99113
log.Printf("Mark as unhealthy")
100114
forceUnhealthy = new(bool)
@@ -121,17 +135,29 @@ func main(cmd *cobra.Command, args []string) {
121135

122136
serverAdr := fmt.Sprintf(":%d", port)
123137
listenAddr, err := net.Listen("tcp", serverAdr)
138+
124139
if err != nil {
125-
log.Fatal(fmt.Sprintf("Error while starting the listening service %v", err.Error()))
140+
log.Fatalf("Error while starting the listening service %v", err)
141+
}
142+
143+
var grpcServer *grpc.Server
144+
145+
if certFile != "" && privKeyFile != "" {
146+
creds, err := credentials.NewServerTLSFromFile(certFile, privKeyFile)
147+
if err != nil {
148+
log.Fatalf("Failed to generate credentials %v", err)
149+
}
150+
grpcServer = grpc.NewServer(grpc.Creds(creds))
151+
} else {
152+
grpcServer = grpc.NewServer()
126153
}
127154

128-
grpcServer := grpc.NewServer()
129155
healthService := NewHealthChecker(started)
130156
grpc_health_v1.RegisterHealthServer(grpcServer, healthService)
131157

132158
log.Printf("gRPC server starting to listen on %s", serverAdr)
133159
if err = grpcServer.Serve(listenAddr); err != nil {
134-
log.Fatal(fmt.Sprintf("Error while starting the gRPC server on the %s listen address %v", listenAddr, err.Error()))
160+
log.Fatalf("Error while starting the gRPC server on the %s listen address %v", listenAddr, err)
135161
}
136162

137163
select {}

0 commit comments

Comments
 (0)