Skip to content

Commit

Permalink
cmd: Add health check commands (#319)
Browse files Browse the repository at this point in the history
Co-Authored-By: hackerman <[email protected]>
  • Loading branch information
tleef and aeneasr committed Dec 18, 2019
1 parent 083b7eb commit 0dd3fe3
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cmd/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// healthCmd represents the health command
var healthCmd = &cobra.Command{
Use: "health",
Short: "Commands for checking the status of an ORY Oathkeeper deployment",
Long: `Note:
The endpoint URL should point to a single ORY Oathkeeper deployment.
If the endpoint URL points to a Load Balancer, these commands will effective test the Load Balancer.
`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(cmd.UsageString())
},
}

func init() {
RootCmd.AddCommand(healthCmd)
healthCmd.PersistentFlags().StringP("endpoint", "e", "", "The endpoint URL of ORY Oathkeeper's management API")
}
40 changes: 40 additions & 0 deletions cmd/health_alive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cmd

import (
"fmt"
"os"

"github.com/ory/oathkeeper/sdk/go/oathkeeper/client/api"
"github.com/ory/x/cmdx"
"github.com/spf13/cobra"
)

// healthCmd represents the health command
var healthAliveCmd = &cobra.Command{
Use: "alive",
Short: "Checks if an ORY Oathkeeper deployment is alive",
Long: `Usage example:
oathkeeper health --endpoint=http://localhost:4456/ alive
Note:
The endpoint URL should point to a single ORY Oathkeeper deployment.
If the endpoint URL points to a Load Balancer, these commands will effective test the Load Balancer.
`,
Run: func(cmd *cobra.Command, args []string) {
client := newClient(cmd)

r, err := client.API.IsInstanceAlive(api.NewIsInstanceAliveParams())
// If err, print err and exit 1
cmdx.Must(err, "%s", err)
// Print payload
fmt.Println(cmdx.FormatResponse(r.Payload))
// When healthy, ORY Oathkeeper always returns a status of "ok"
if r.Payload.Status != "ok" {
os.Exit(1)
}
},
}

func init() {
healthCmd.AddCommand(healthAliveCmd)
}
40 changes: 40 additions & 0 deletions cmd/health_ready.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cmd

import (
"fmt"
"os"

"github.com/ory/oathkeeper/sdk/go/oathkeeper/client/api"
"github.com/ory/x/cmdx"
"github.com/spf13/cobra"
)

// healthCmd represents the health command
var healthReadyCmd = &cobra.Command{
Use: "ready",
Short: "Checks if an ORY Oathkeeper deployment is ready",
Long: `Usage example:
oathkeeper health --endpoint=http://localhost:4456/ ready
Note:
The endpoint URL should point to a single ORY Oathkeeper deployment.
If the endpoint URL points to a Load Balancer, these commands will effective test the Load Balancer.
`,
Run: func(cmd *cobra.Command, args []string) {
client := newClient(cmd)

r, err := client.API.IsInstanceReady(api.NewIsInstanceReadyParams())
// If err, print err and exit 1
cmdx.Must(err, "%s", err)
// Print payload
fmt.Println(cmdx.FormatResponse(r.Payload))
// When healthy, ORY Oathkeeper always returns a status of "ok"
if r.Payload.Status != "ok" {
os.Exit(1)
}
},
}

func init() {
healthCmd.AddCommand(healthReadyCmd)
}
2 changes: 2 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func TestCommandLineInterface(t *testing.T) {
},
{args: []string{"rules", fmt.Sprintf("--endpoint=http://127.0.0.1:%d/", apiPort), "list"}},
{args: []string{"rules", fmt.Sprintf("--endpoint=http://127.0.0.1:%d/", apiPort), "get", "test-rule-4"}},
{args: []string{"health", fmt.Sprintf("--endpoint=http://127.0.0.1:%d/", apiPort), "alive"}},
{args: []string{"health", fmt.Sprintf("--endpoint=http://127.0.0.1:%d/", apiPort), "ready"}},
{args: []string{"credentials", "generate", "--alg", "RS256"}},
{args: []string{"credentials", "generate", "--alg", "ES256"}},
{args: []string{"credentials", "generate", "--alg", "HS256"}},
Expand Down

0 comments on commit 0dd3fe3

Please sign in to comment.