-
Notifications
You must be signed in to change notification settings - Fork 3
Add support for deploying app in kubernetes #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "database/sql" | ||
| "errors" | ||
| "flag" | ||
| "log" | ||
| "net" | ||
| "time" | ||
|
|
||
| "github.com/golang-migrate/migrate/v4" | ||
| "github.com/golang-migrate/migrate/v4/database/postgres" | ||
| _ "github.com/golang-migrate/migrate/v4/source/file" // Necessary to invoke migrations from files | ||
| config "github.com/rhmdnd/compserv/pkg/config" | ||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| func main() { | ||
| configDir := flag.String("config-dir", "configs/", | ||
| "Path to YAML configuration directory containing a config.yaml file.") | ||
| configFile := flag.String("config-file", "config.yaml", | ||
| "File name of the service config") | ||
| flag.Parse() | ||
| v := config.ParseConfig(*configDir, *configFile) | ||
| db := getDatabaseConnection(v) | ||
| log.Printf("Connected to database: %v", v.GetString("database.host")) | ||
| driver, err := postgres.WithInstance(db, &postgres.Config{}) | ||
| if err != nil { | ||
| log.Fatalf("Unable to initialize database driver for migrations: %s", err) | ||
| } | ||
| // The file path to the migration could be a configuration option, but | ||
| // I'm not sure how useful that would be since they're copied into the | ||
| // container during the build process. Might only be useful for people | ||
| // building their own container images. | ||
| m, err := migrate.NewWithDatabaseInstance("file:///app/migrations", "postgres", driver) | ||
| if err != nil { | ||
| log.Fatalf("Unable to initialize migrations: %s", err) | ||
| } | ||
| if err := m.Up(); err != nil { | ||
| log.Fatalf("Unable to upgrade the database: %s", err) | ||
| } | ||
| version, _, err := m.Version() | ||
| if err != nil { | ||
| log.Fatalf("Unable to determine database version: %s", err) | ||
| } | ||
| log.Printf("Database successful migrated to version %d", version) | ||
| } | ||
|
|
||
| func getDatabaseConnection(v *viper.Viper) *sql.DB { | ||
rhmdnd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // This should be updated so that we don't have to disable ssl | ||
| connStr := config.GetDatabaseConnectionString(v) + " sslmode=disable" | ||
|
|
||
| db, err := sql.Open("postgres", connStr) | ||
| if err != nil { | ||
| log.Fatalf("Unable to initialize connection to database: %s", err) | ||
| } | ||
|
|
||
| // Wait up to 30 seconds to establish a connection with the database. | ||
| // Remove this logic when we have the ability to set retries in the | ||
| // database connection directly | ||
| // (https://github.com/golang/go/issues/48309). | ||
| for i := 0; i < 10; i++ { | ||
rhmdnd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err := db.Ping(); err != nil { | ||
| // We should only retry if we're dealing with a network | ||
| // issue of some kind. No amount of retries is going to | ||
| // fix incorrect credentials. | ||
| var netError *net.OpError | ||
| if errors.As(err, &netError) { | ||
| log.Fatalf("Retrying database connection due to error: %s", err) | ||
| // Linting says we shouldn't use the following: | ||
| // time.Sleep(3 * time.Second) | ||
| // but we can't use | ||
| // duration := 3 | ||
| // time.Sleep(duration * time.Second) | ||
| // which causes a type mismatch. | ||
| duration, _ := time.ParseDuration("0m3s") | ||
| time.Sleep(duration) | ||
| continue | ||
| } else { | ||
| log.Fatalf("Unable to establish connection to database: %s", err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return db | ||
| } | ||
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| kind: ConfigMap | ||
| apiVersion: v1 | ||
| metadata: | ||
| name: compserv-config | ||
| namespace: compserv | ||
| data: | ||
| config: | | ||
rhmdnd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| database: | ||
| host: "postgres.compserv" | ||
| username: "postgres" | ||
| password: | ||
| provider: "kubernetes" | ||
| secret_name: "postgres-secret" | ||
| secret_namespace: "compserv" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| namespace: compserv | ||
| resources: | ||
| - namespace.yaml | ||
| - deployment.yaml | ||
| - service.yaml | ||
| - service-account.yaml | ||
| - persistent-disk.yaml | ||
| - deployment.yaml | ||
| - secret.yaml | ||
|
|
||
| - configmap.yaml | ||
| - rbac.yaml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # The volume created dynamically from this claim will be reclaimed when the pod | ||
| # is gone. Is that the behavior we want or do we want to create a persistent | ||
| # volume directly and use that? | ||
| --- | ||
| apiVersion: v1 | ||
| kind: PersistentVolumeClaim | ||
| metadata: | ||
| labels: | ||
| app: postgres | ||
| name: postgres-pvc | ||
| spec: | ||
| storageClassName: "gp2" | ||
rhmdnd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| accessModes: | ||
| - ReadWriteOnce | ||
| resources: | ||
| requests: | ||
| storage: 20Gi | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| --- | ||
| kind: Role | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| metadata: | ||
| name: compserv-service-account-role | ||
rhmdnd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| rules: | ||
| - apiGroups: | ||
| - "" | ||
| resources: | ||
| - secrets | ||
| verbs: | ||
| - get | ||
| --- | ||
| kind: RoleBinding | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| metadata: | ||
| name: compserv-service-account-role | ||
| subjects: | ||
| - kind: ServiceAccount | ||
| name: compserv-sa | ||
| namespace: compserv | ||
rhmdnd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| roleRef: | ||
| kind: Role | ||
| name: compserv-service-account-role | ||
| apiGroup: rbac.authorization.k8s.io | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
|
|
||
| --- | ||
| kind: ServiceAccount | ||
| apiVersion: v1 | ||
| metadata: | ||
| name: compserv-sa |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: this could be done with the kustomize
set imagecommand rather than sed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh - nice, thanks!
I'll propose a follow up.