Skip to content

Commit

Permalink
Merge pull request #141 from dagbay-rh/local_devopment_improvements
Browse files Browse the repository at this point in the history
local development improvements
  • Loading branch information
lpichler authored Aug 24, 2023
2 parents 96b2f8b + 5a340c9 commit 3b42c1f
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 90 deletions.
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

19 changes: 3 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,16 @@ generate: $(gen_files)

image:
podman build -t entitlements-api-go .
exe: all
./entitlements-api-go
debug-run: generate
ENT_DEBUG=1 \
ENT_CA_PATH=$(PWD)/resources/ca.crt \
ENT_KEY=$(PWD)/test_data/test.key \
ENT_CERT=$(PWD)/test_data/test.cert \
go run main.go
run: generate
ENT_CA_PATH=$(PWD)/resources/ca.crt \
ENT_KEY=$(PWD)/test_data/test.key \
ENT_CERT=$(PWD)/test_data/test.cert \
go run main.go
test: generate
ENT_CA_PATH=$(PWD)/resources/ca.crt \
ENT_KEY=$(PWD)/test_data/test.key \
ENT_CERT=$(PWD)/test_data/test.cert \
go test -v ./...
test-all: generate
ENT_CA_PATH=$(PWD)/resources/ca.crt \
ENT_KEY=$(PWD)/test_data/test.key \
ENT_CERT=$(PWD)/test_data/test.cert \
go test --race --coverprofile=coverage.out --covermode=atomic ./...
go test -v --race --coverprofile=coverage.txt --covermode=atomic ./...
bench: generate
ENT_CA_PATH=$(PWD)/resources/ca.crt \
ENT_KEY=$(PWD)/test_data/test.key \
ENT_CERT=$(PWD)/test_data/test.cert \
go test -bench=. ./...
50 changes: 43 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Entitlements Service

Entitlements service serves as a proxy to various backend Red Hat IT services. It performs the following functions:
* `/subscriptions`: query IT for a list of subscriptions that a user is entitled to
* `/compliance`: query IT for user compliance checks
* `/seats`: query AMS from OCM to read, assign, and delete user subscriptions (a seat is considered an Openshift subscription assignable to a user)

## SKU/Bundle changes
- The `/bundles/bundles.example.yml` file in this repo is for **local testing only**
- To run the app, be sure to copy `/bundles/bundles.example.yml` to `/bundles/bundles.yml`
Expand All @@ -22,7 +27,7 @@ git clone git@github.com:RedHatInsights/entitlements-api-go.git
Then, install the project's Go dependencies by running:

```sh
bash ./scripts/dev_deps.sh
go get ./...
```

Build the project and generate the openapi types and stubs:
Expand Down Expand Up @@ -56,9 +61,12 @@ export ENT_CERT=./{path_to_cert}.crt
export ENT_CA_PATH=./{path_to_ca_cert}.crt
export ENT_SUBS_HOST=https://subscription.dev.api.redhat.com
export ENT_COMPLIANCE_HOST=https://export-compliance.dev.api.redhat.com
export ENT_DEBUG=true
```

Replace `{path_to_key}` and `{path_to_cert}` with the locations of the `.key` and `.crt` files from the previous section.
ENT_DEBUG will use mock clients for ams and bop, if this is not set to true you will need more config to setup those clients.
See the bop and ams client files for what config is needed.

### Set up your local entitlement bundles

Expand All @@ -71,7 +79,18 @@ Copy the `/bundles/bundles.example.yml` to `/bundles/bundles.yml` in order to ha
Now that everything is set up, you can run the application using:

```bash
bash ./scripts/watch.sh ./local/development.env.sh
source ./local/development.env.sh
```
then run one of the following

```bash
# this will generate, build, and run the built executable (good for debugging)
make exe
```
or you can also run
```bash
# this will run `go run ...` which will build a stripped down optimized version of the app
make run
```

To run locally with Docker:
Expand All @@ -90,15 +109,32 @@ For an example see `cat ./scripts/xrhid_helper.sh`

To test the bundle sync behavior, you'll need to configure your environment similar to the instructions above, build the script, and run it against the dev environment:

```sh
. ./local/development.env.sh
go build -o ./bundle-sync bundle_sync/main.go
```bash
source ./local/development.env.sh
make build
./bundle-sync
```

## Running the Unit Tests

* To run the unit tests, execute the following commands from the terminal:
`make test`
```bash
make test
```
* To run the unit tests how they are run in our pr_check builds (generates output files):
```bash
make test-all
```
* To include benchmarks:
`make bench`
```bash
make bench
```

## Releases

**Versioning:** All releases should follow [SemVer](http://semver.org) versioning.

**Release Tagging:** Releases are drafted with a version, and then `master` is tagged with the version number.
For example, release [`Entitlements v1.16.1`](https://github.com/RedHatInsights/entitlements-api-go/releases/tag/v1.16.1) has a corresponding tag [`v1.16.1`](https://github.com/RedHatInsights/entitlements-api-go/tree/v1.16.1).

**Deployment:** Once a PR merges to master, our [build-main](https://ci.ext.devshift.net/job/RedHatInsights-entitlements-api-go-gh-build-master/) jenkins job will build/deploy an image to `quay.io` and tag it with the commit sha. We then update the image tag of our deployment in app-interface to that same commit sha.
2 changes: 1 addition & 1 deletion bop/client_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
. "github.com/onsi/gomega"
)

func TestControllers(t *testing.T) {
func TestBop(t *testing.T) {
InitLogger()
RegisterFailHandler(Fail)
RunSpecs(t, "BOP Suite")
Expand Down
26 changes: 12 additions & 14 deletions bundle_sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
Expand All @@ -13,7 +13,7 @@ import (
"strings"
"time"

cfg "github.com/RedHatInsights/entitlements-api-go/config"
"github.com/RedHatInsights/entitlements-api-go/config"
t "github.com/RedHatInsights/entitlements-api-go/types"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
Expand All @@ -25,7 +25,7 @@ func assertEq(test []string, ans []string) bool {
}

// getClient sets up the http client for the subscriptions API
func getClient(cfg *cfg.EntitlementsConfig) *http.Client {
func getClient(cfg *config.EntitlementsConfig) *http.Client {

tlsConfig := &tls.Config{
Certificates: []tls.Certificate{*cfg.Certs},
Expand Down Expand Up @@ -59,7 +59,7 @@ func getCurrent(client *http.Client, url string) (t.SubModel, error) {
}
defer resp.Body.Close()

data, err := ioutil.ReadAll(resp.Body)
data, err := io.ReadAll(resp.Body)
if err != nil {
return t.SubModel{}, err
}
Expand All @@ -73,7 +73,7 @@ func getCurrent(client *http.Client, url string) (t.SubModel, error) {
}

func getUpdates(cfg *viper.Viper) ([]t.Bundle, error) {
bundlesYaml, err := ioutil.ReadFile(cfg.GetString("BUNDLE_INFO_YAML"))
bundlesYaml, err := os.ReadFile(cfg.GetString(config.Keys.BundleInfoYaml))
if err != nil {
return []t.Bundle{}, err
}
Expand All @@ -89,7 +89,7 @@ func getUpdates(cfg *viper.Viper) ([]t.Bundle, error) {
}

func postUpdates(cfg *viper.Viper, client *http.Client, data []byte) error {
url := fmt.Sprintf("%s%s%s", cfg.GetString("SUBS_HOST"), cfg.GetString("SUB_API_BASE_PATH"), "features/")
url := fmt.Sprintf("%s%s%s", cfg.GetString(config.Keys.SubsHost), cfg.GetString(config.Keys.SubAPIBasePath), "features/")
req, err := client.Post(url, "application/json", strings.NewReader(string(data)))
if err != nil {
return err
Expand All @@ -101,23 +101,23 @@ func postUpdates(cfg *viper.Viper, client *http.Client, data []byte) error {
}

func main() {
c := cfg.GetConfig()
c := config.GetConfig()
client := getClient(c)
options := c.Options
runSync := options.GetBool(cfg.Keys.RunBundleSync)
runSync := options.GetBool(config.Keys.RunBundleSync)

if !runSync {
fmt.Println("Bundle sync disabled")
return
}

endpoints := strings.Split(c.Options.GetString(cfg.Keys.Features), ",")
endpoints := strings.Split(c.Options.GetString(config.Keys.Features), ",")
for _, endpoint := range endpoints {
skus := make(map[string][]string)
current_skus := make(map[string][]string)
url := fmt.Sprintf("%s%s%s",
options.GetString("SUBS_HOST"),
options.GetString("SUB_API_BASE_PATH"),
options.GetString(config.Keys.SubsHost),
options.GetString(config.Keys.SubAPIBasePath),
"features/")
current, err := getCurrent(client, url+endpoint)
if err != nil {
Expand All @@ -132,9 +132,7 @@ func main() {
}
for _, v := range sku_updates {
if v.Name == endpoint {
for _, sku := range v.Skus {
skus[endpoint] = append(skus[endpoint], sku)
}
skus[endpoint] = append(skus[endpoint], v.Skus...)
}
}

Expand Down
18 changes: 14 additions & 4 deletions config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"crypto/x509"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/spf13/viper"
Expand Down Expand Up @@ -128,7 +130,7 @@ func getRootCAs(localCertFile string) *x509.CertPool {
}

func loadCerts(options *viper.Viper) (tls.Certificate, error) {
if options.GetBool("CERTS_FROM_ENV") == true {
if options.GetBool(Keys.CertsFromEnv) {
return tls.X509KeyPair(
[]byte(options.GetString(Keys.Cert)),
[]byte(options.GetString(Keys.Key)),
Expand Down Expand Up @@ -157,14 +159,22 @@ func initialize() {
hostname = "entitlements"
}

wd := "."
_, filename, _, ok := runtime.Caller(0)
if !ok {
fmt.Printf("Error getting runtime caller, some default config settings might not be set correctly. Working directory set to: [%s]\n", wd)
} else {
wd = filepath.Dir(filename)
}

options.SetDefault(Keys.CertsFromEnv, false)
options.SetDefault(Keys.Port, "3000")
options.SetDefault(Keys.LogLevel, "info")
options.SetDefault(Keys.SubsHost, "https://subscription.api.redhat.com")
options.SetDefault(Keys.ComplianceHost, "https://export-compliance.api.redhat.com")
options.SetDefault(Keys.CaPath, "../resources/ca.crt")
options.SetDefault(Keys.Cert, "../test_data/test.cert") // default values of Cert and Key are for testing purposes only
options.SetDefault(Keys.Key, "../test_data/test.key")
options.SetDefault(Keys.CaPath, fmt.Sprintf("%s/../resources/ca.crt", wd))
options.SetDefault(Keys.Cert, fmt.Sprintf("%s/../test_data/test.cert", wd)) // default values of Cert and Key are for testing purposes only
options.SetDefault(Keys.Key, fmt.Sprintf("%s/../test_data/test.key", wd))
options.SetDefault(Keys.OpenAPISpecPath, "./apispec/api.spec.json")
options.SetDefault(Keys.BundleInfoYaml, "./bundles/bundles.yml")
options.SetDefault(Keys.CwLogGroup, "platform-dev")
Expand Down
3 changes: 0 additions & 3 deletions modd.conf

This file was deleted.

17 changes: 14 additions & 3 deletions pr_check.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

# --------------------------------------------
# Options that must be configured by app owner
# --------------------------------------------
export APP_NAME="entitlements" # name of app-sre "application" folder this component lives in
export COMPONENT_NAME="entitlements-api-go" # name of app-sre "resourceTemplate" in deploy.yaml for this component
export IMAGE="quay.io/cloudservices/entitlements-api-go" # the image location on quay
Expand All @@ -11,9 +14,17 @@ curl -s $CICD_URL/bootstrap.sh > .cicd_bootstrap.sh && source .cicd_bootstrap.sh
# Build the image and push to quay
source $CICD_ROOT/build.sh

# NOP action until PR Check logic is added in the future
mkdir -p $WORKSPACE/artifacts
cat << EOF > $WORKSPACE/artifacts/junit-dummy.xml
make test-all
if [ $? != 0 ]; then
exit 1
fi

cp coverage.txt $ARTIFACTS_DIR

# manually paste dummy PR Check results until we setup iqe tests to run in build and they post results
# see here for an example: https://github.com/RedHatInsights/insights-ingress-go/blob/master/pr_check.sh#L23-L25
# with cji_smoke_test and post_test_results, we can run iqe tests and they will post results to this dir
cat << EOF > $ARTIFACTS_DIR/junit-dummy.xml
<testsuite tests="1">
<testcase classname="dummy" name="dummytest"/>
</testsuite>
Expand Down
11 changes: 0 additions & 11 deletions scripts/dev_deps.sh

This file was deleted.

14 changes: 0 additions & 14 deletions scripts/watch.sh

This file was deleted.

8 changes: 5 additions & 3 deletions server/routes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package server

import (
"fmt"

chilogger "github.com/766b/chi-logger"
"github.com/RedHatInsights/entitlements-api-go/ams"
"github.com/RedHatInsights/entitlements-api-go/api"
Expand Down Expand Up @@ -39,12 +41,12 @@ func DoRoutes() chi.Router {
amsClient, err := ams.NewClient(debug)

if err != nil {
panic(err)
panic(fmt.Sprintf("Error constructing ams client: [%s]", err))
}

bopClient, err := bop.NewClient(debug)
if err != nil {
panic(err)
panic(fmt.Sprintf("Error constructing bop client: [%s]", err))
}

// This is odd, but the generated code will register handlers
Expand All @@ -53,7 +55,7 @@ func DoRoutes() chi.Router {
// a way to hack it in
if !configOptions.GetBool(config.Keys.DisableSeatManager) {
seatManagerApi := controllers.NewSeatManagerApi(amsClient, bopClient)
api.HandlerFromMuxWithBaseURL(seatManagerApi, r.With(identity.EnforceIdentity), "/api/entitlements/v1")
api.HandlerFromMuxWithBaseURL(seatManagerApi, r.With(identity.EnforceIdentity), "/api/entitlements/v1")
}

r.Route("/api/entitlements/v1", func(r chi.Router) {
Expand Down

0 comments on commit 3b42c1f

Please sign in to comment.