diff --git a/.github/workflows/build_container.yaml b/.github/workflows/build_container.yaml index 1a98778..e8e8cda 100644 --- a/.github/workflows/build_container.yaml +++ b/.github/workflows/build_container.yaml @@ -33,7 +33,7 @@ jobs: steps: - name: Check out code - uses: actions/checkout@24cb9080177205b6e8c946b17badbe402adc938f # v3 + uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3 - name: Extract branch name id: extract_branch @@ -70,7 +70,7 @@ jobs: RELEASE_REGISTRY: "localhost:5000/k8sgpt" steps: - name: Check out code - uses: actions/checkout@24cb9080177205b6e8c946b17badbe402adc938f # v3 + uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3 - name: Set up Docker Buildx id: buildx @@ -115,7 +115,7 @@ jobs: contents: read # Needed for checking out the repository steps: - name: Check out code - uses: actions/checkout@24cb9080177205b6e8c946b17badbe402adc938f # v3 + uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3 - name: Login to GitHub Container Registry uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a # v2 diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 62b9c6e..737d912 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -23,7 +23,7 @@ jobs: # Release-please creates a PR that tracks all changes steps: - name: Checkout - uses: actions/checkout@24cb9080177205b6e8c946b17badbe402adc938f # v3 + uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3 - uses: google-github-actions/release-please-action@e0b9d1885d92e9a93d5ce8656de60e3b806e542c # v3 id: release @@ -42,7 +42,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@24cb9080177205b6e8c946b17badbe402adc938f # v3 + uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3 with: fetch-depth: 0 - name: Set up Go @@ -57,7 +57,7 @@ jobs: version: latest args: release --clean env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_TOKEN: ${{ secrets.K8SGPT_BOT_SECRET }} build-container: if: needs.release-please.outputs.releases_created == 'true' @@ -72,7 +72,7 @@ jobs: IMAGE_TAG: ghcr.io/k8sgpt-ai/k8sgpt:${{ needs.release-please.outputs.tag_name }} steps: - name: Checkout - uses: actions/checkout@24cb9080177205b6e8c946b17badbe402adc938f # v3 + uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3 with: submodules: recursive diff --git a/.goreleaser.yaml b/.goreleaser.yaml index d5574b6..71a9919 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -29,6 +29,13 @@ archives: - goos: windows format: zip +brews: + - name: k8sgpt + homepage: https://k8sgpt.ai + tap: + owner: k8sgpt-ai + name: homebrew-k8sgpt + checksum: name_template: 'checksums.txt' diff --git a/.release-please-manifest.json b/.release-please-manifest.json index d035328..729b0ad 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1 +1 @@ -{".":"0.0.3"} \ No newline at end of file +{".":"0.0.4"} \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 59c2846..96634f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,127 @@ # Changelog +## [0.0.4](https://github.com/k8sgpt-ai/k8sgpt/compare/v0.0.4...v0.0.4) (2023-03-24) + + +### Features + +* build container ([260640f](https://github.com/k8sgpt-ai/k8sgpt/commit/260640f865baefba8ac256f800d4992f25ca15fd)) +* find replicaset errors ([8ac56e0](https://github.com/k8sgpt-ai/k8sgpt/commit/8ac56e062baef2a0cf7c7ce2b4c97753f079f157)) + + +### Bug Fixes + +* add permissions to read repository ([d6cc4cf](https://github.com/k8sgpt-ai/k8sgpt/commit/d6cc4cfcbffbf84f27c7e4e4159da1e42dd5d689)) +* build ([1fbed3e](https://github.com/k8sgpt-ai/k8sgpt/commit/1fbed3e44ff790fccfef502ddafae92e34629c21)) +* container naming ([115276e](https://github.com/k8sgpt-ai/k8sgpt/commit/115276e01a38fc1692d6b66ab56a33f1e1793974)) +* **deps:** update module github.com/sashabaranov/go-openai to v1.5.5 ([105fe44](https://github.com/k8sgpt-ai/k8sgpt/commit/105fe44680e5a987d4a65ff9c58b5b2211808c5e)) +* **deps:** update module github.com/sashabaranov/go-openai to v1.5.6 ([37a1d3f](https://github.com/k8sgpt-ai/k8sgpt/commit/37a1d3f47e07caddb168f228627973870a9d867e)) +* **deps:** update module github.com/sashabaranov/go-openai to v1.5.7 ([7f7726d](https://github.com/k8sgpt-ai/k8sgpt/commit/7f7726d59a63baeaf8ff110e00b30a20ec7f1df5)) +* minor adaptions ([ef17b84](https://github.com/k8sgpt-ai/k8sgpt/commit/ef17b845ba3c65c16ed5dcc417e3e3d3d40dd04e)) +* release please config ([c402c7b](https://github.com/k8sgpt-ai/k8sgpt/commit/c402c7bab7baababbbc7c82965d8337de7d50d35)) +* remove sboms from goreleaser ([addc01f](https://github.com/k8sgpt-ai/k8sgpt/commit/addc01f700dd2ea31ec24dcf4995bb7ed4a4785e)) + + +### Docs + +* add some important information to contributing ([9ab7f58](https://github.com/k8sgpt-ai/k8sgpt/commit/9ab7f587620d69e4e8fc98faabce6417c35f7497)) +* update CONTRIBUTING ([05a787d](https://github.com/k8sgpt-ai/k8sgpt/commit/05a787d53dfe5e625c6449ac1e21ec36e66ddd28)) +* update CONTRIBUTING ([26449e1](https://github.com/k8sgpt-ai/k8sgpt/commit/26449e10efd8926cccd4a2eaa4e9dc3afa8bd01a)) + + +### Other + +* add bot secret to goreleaser ([171e58b](https://github.com/k8sgpt-ai/k8sgpt/commit/171e58b51107f75717694e35c4e249ee41f0409a)) +* add brew tap generation on release ([2992c4e](https://github.com/k8sgpt-ai/k8sgpt/commit/2992c4e5c8abad50c90ed85523c732f19ab1f31c)) +* add initial renovate config ([e37dbc7](https://github.com/k8sgpt-ai/k8sgpt/commit/e37dbc7909f1c520c4c6660c25b45de5847ea581)) +* add pull request template ([a6d5132](https://github.com/k8sgpt-ai/k8sgpt/commit/a6d5132b8c2ff077680e2edfd8361a93008197fd)) +* add release-please ([da7b409](https://github.com/k8sgpt-ai/k8sgpt/commit/da7b40978d55a6afed4c3a1ca83a756238feaca8)) +* change module repo ([a307c13](https://github.com/k8sgpt-ai/k8sgpt/commit/a307c132b3464ff2e949c8a5588e01d344de91a0)) +* **deps:** pin dependencies ([f6072f5](https://github.com/k8sgpt-ai/k8sgpt/commit/f6072f56cbe2c073b7b7ebef6c12fa98120e54e2)) +* **deps:** pin dependencies ([5b360de](https://github.com/k8sgpt-ai/k8sgpt/commit/5b360de2ae6094cf850a4ae973a22855c21a9040)) +* **deps:** pin dependencies ([7fea7d1](https://github.com/k8sgpt-ai/k8sgpt/commit/7fea7d14a572fe0fd05f5f241b98e93655fb1965)) +* **deps:** update actions/checkout digest to 8f4b7f8 ([9955d75](https://github.com/k8sgpt-ai/k8sgpt/commit/9955d754505b60f28d17397132a1d02e95ffe303)) +* **main:** release 0.0.3 ([53c9947](https://github.com/k8sgpt-ai/k8sgpt/commit/53c994725ea2c2c54898ffe5307d9df40e9c1fe5)) +* **main:** release 0.0.3 ([f5d8609](https://github.com/k8sgpt-ai/k8sgpt/commit/f5d86092f49faef8d71cb950986d76c3f92daf46)) +* **main:** release 0.0.3 ([22873a6](https://github.com/k8sgpt-ai/k8sgpt/commit/22873a67163e58484d2a0ad343b4ba3c83e51d8f)) +* **main:** release 0.0.4 ([aef7256](https://github.com/k8sgpt-ai/k8sgpt/commit/aef7256dc3a85817573744f8b4a54f834368bac7)) +* **main:** release 0.0.4 ([6dbcde9](https://github.com/k8sgpt-ai/k8sgpt/commit/6dbcde94e961a6e5a1fc0559d2a1da5567a659de)) +* release 0.0.3 ([4840aa0](https://github.com/k8sgpt-ai/k8sgpt/commit/4840aa081e3aa4a7a01fd3fd5f837fa6f0c3c02c)) +* release 0.0.3 ([de02795](https://github.com/k8sgpt-ai/k8sgpt/commit/de027955ea18a751c5f991e7ff0f60b90ae704b0)) +* release 0.0.3 ([a927c32](https://github.com/k8sgpt-ai/k8sgpt/commit/a927c32def806bb8b99e1cfcd4ee3dcdeca6ae5d)) +* release 0.0.4 ([08f2c31](https://github.com/k8sgpt-ai/k8sgpt/commit/08f2c3112e2cc16b49b9cf8fdbd97368acecc754)) + +## [0.0.4](https://github.com/k8sgpt-ai/k8sgpt/compare/v0.0.4...v0.0.4) (2023-03-24) + + +### Features + +* build container ([260640f](https://github.com/k8sgpt-ai/k8sgpt/commit/260640f865baefba8ac256f800d4992f25ca15fd)) +* find replicaset errors ([8ac56e0](https://github.com/k8sgpt-ai/k8sgpt/commit/8ac56e062baef2a0cf7c7ce2b4c97753f079f157)) + + +### Bug Fixes + +* add permissions to read repository ([d6cc4cf](https://github.com/k8sgpt-ai/k8sgpt/commit/d6cc4cfcbffbf84f27c7e4e4159da1e42dd5d689)) +* build ([1fbed3e](https://github.com/k8sgpt-ai/k8sgpt/commit/1fbed3e44ff790fccfef502ddafae92e34629c21)) +* container naming ([115276e](https://github.com/k8sgpt-ai/k8sgpt/commit/115276e01a38fc1692d6b66ab56a33f1e1793974)) +* **deps:** update module github.com/sashabaranov/go-openai to v1.5.5 ([105fe44](https://github.com/k8sgpt-ai/k8sgpt/commit/105fe44680e5a987d4a65ff9c58b5b2211808c5e)) +* **deps:** update module github.com/sashabaranov/go-openai to v1.5.6 ([37a1d3f](https://github.com/k8sgpt-ai/k8sgpt/commit/37a1d3f47e07caddb168f228627973870a9d867e)) +* **deps:** update module github.com/sashabaranov/go-openai to v1.5.7 ([7f7726d](https://github.com/k8sgpt-ai/k8sgpt/commit/7f7726d59a63baeaf8ff110e00b30a20ec7f1df5)) +* minor adaptions ([ef17b84](https://github.com/k8sgpt-ai/k8sgpt/commit/ef17b845ba3c65c16ed5dcc417e3e3d3d40dd04e)) +* release please config ([c402c7b](https://github.com/k8sgpt-ai/k8sgpt/commit/c402c7bab7baababbbc7c82965d8337de7d50d35)) +* remove sboms from goreleaser ([addc01f](https://github.com/k8sgpt-ai/k8sgpt/commit/addc01f700dd2ea31ec24dcf4995bb7ed4a4785e)) + + +### Docs + +* add some important information to contributing ([9ab7f58](https://github.com/k8sgpt-ai/k8sgpt/commit/9ab7f587620d69e4e8fc98faabce6417c35f7497)) +* update CONTRIBUTING ([05a787d](https://github.com/k8sgpt-ai/k8sgpt/commit/05a787d53dfe5e625c6449ac1e21ec36e66ddd28)) +* update CONTRIBUTING ([26449e1](https://github.com/k8sgpt-ai/k8sgpt/commit/26449e10efd8926cccd4a2eaa4e9dc3afa8bd01a)) + + +### Other + +* add bot secret to goreleaser ([171e58b](https://github.com/k8sgpt-ai/k8sgpt/commit/171e58b51107f75717694e35c4e249ee41f0409a)) +* add brew tap generation on release ([2992c4e](https://github.com/k8sgpt-ai/k8sgpt/commit/2992c4e5c8abad50c90ed85523c732f19ab1f31c)) +* add initial renovate config ([e37dbc7](https://github.com/k8sgpt-ai/k8sgpt/commit/e37dbc7909f1c520c4c6660c25b45de5847ea581)) +* add pull request template ([a6d5132](https://github.com/k8sgpt-ai/k8sgpt/commit/a6d5132b8c2ff077680e2edfd8361a93008197fd)) +* add release-please ([da7b409](https://github.com/k8sgpt-ai/k8sgpt/commit/da7b40978d55a6afed4c3a1ca83a756238feaca8)) +* change module repo ([a307c13](https://github.com/k8sgpt-ai/k8sgpt/commit/a307c132b3464ff2e949c8a5588e01d344de91a0)) +* **deps:** pin dependencies ([f6072f5](https://github.com/k8sgpt-ai/k8sgpt/commit/f6072f56cbe2c073b7b7ebef6c12fa98120e54e2)) +* **deps:** pin dependencies ([5b360de](https://github.com/k8sgpt-ai/k8sgpt/commit/5b360de2ae6094cf850a4ae973a22855c21a9040)) +* **deps:** pin dependencies ([7fea7d1](https://github.com/k8sgpt-ai/k8sgpt/commit/7fea7d14a572fe0fd05f5f241b98e93655fb1965)) +* **deps:** update actions/checkout digest to 8f4b7f8 ([9955d75](https://github.com/k8sgpt-ai/k8sgpt/commit/9955d754505b60f28d17397132a1d02e95ffe303)) +* **main:** release 0.0.3 ([53c9947](https://github.com/k8sgpt-ai/k8sgpt/commit/53c994725ea2c2c54898ffe5307d9df40e9c1fe5)) +* **main:** release 0.0.3 ([f5d8609](https://github.com/k8sgpt-ai/k8sgpt/commit/f5d86092f49faef8d71cb950986d76c3f92daf46)) +* **main:** release 0.0.3 ([22873a6](https://github.com/k8sgpt-ai/k8sgpt/commit/22873a67163e58484d2a0ad343b4ba3c83e51d8f)) +* **main:** release 0.0.4 ([6dbcde9](https://github.com/k8sgpt-ai/k8sgpt/commit/6dbcde94e961a6e5a1fc0559d2a1da5567a659de)) +* release 0.0.3 ([4840aa0](https://github.com/k8sgpt-ai/k8sgpt/commit/4840aa081e3aa4a7a01fd3fd5f837fa6f0c3c02c)) +* release 0.0.3 ([de02795](https://github.com/k8sgpt-ai/k8sgpt/commit/de027955ea18a751c5f991e7ff0f60b90ae704b0)) +* release 0.0.3 ([a927c32](https://github.com/k8sgpt-ai/k8sgpt/commit/a927c32def806bb8b99e1cfcd4ee3dcdeca6ae5d)) +* release 0.0.4 ([08f2c31](https://github.com/k8sgpt-ai/k8sgpt/commit/08f2c3112e2cc16b49b9cf8fdbd97368acecc754)) + +## [0.0.4](https://github.com/k8sgpt-ai/k8sgpt/compare/v0.0.3...v0.0.4) (2023-03-24) + + +### Bug Fixes + +* **deps:** update module github.com/sashabaranov/go-openai to v1.5.7 ([7f7726d](https://github.com/k8sgpt-ai/k8sgpt/commit/7f7726d59a63baeaf8ff110e00b30a20ec7f1df5)) + + +### Docs + +* add some important information to contributing ([9ab7f58](https://github.com/k8sgpt-ai/k8sgpt/commit/9ab7f587620d69e4e8fc98faabce6417c35f7497)) +* update CONTRIBUTING ([05a787d](https://github.com/k8sgpt-ai/k8sgpt/commit/05a787d53dfe5e625c6449ac1e21ec36e66ddd28)) +* update CONTRIBUTING ([26449e1](https://github.com/k8sgpt-ai/k8sgpt/commit/26449e10efd8926cccd4a2eaa4e9dc3afa8bd01a)) + + +### Other + +* add bot secret to goreleaser ([171e58b](https://github.com/k8sgpt-ai/k8sgpt/commit/171e58b51107f75717694e35c4e249ee41f0409a)) +* add brew tap generation on release ([2992c4e](https://github.com/k8sgpt-ai/k8sgpt/commit/2992c4e5c8abad50c90ed85523c732f19ab1f31c)) +* **deps:** update actions/checkout digest to 8f4b7f8 ([9955d75](https://github.com/k8sgpt-ai/k8sgpt/commit/9955d754505b60f28d17397132a1d02e95ffe303)) + ## [0.0.3](https://github.com/k8sgpt-ai/k8sgpt/compare/v0.0.3...v0.0.3) (2023-03-23) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f5d47fe..1c00217 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,5 +1,113 @@ # Contributing +We're happy that you want to contribute to this project. Please read the sections to make the process as smooth as possible. ## Requirements - - Golang `1.20` +- An OpenAI API key + * OpenAI API keys can be obtained from [OpenAI](https://platform.openai.com/account/api-keys) + * You can set the API key for k8sgpt using `./k8sgpt auth key` +- If you want to build the container image, you need to have a container engine (docker, podman, rancher, etc.) installed + +## Getting Started + +**Where should I start?** +- If you are new to the project, please check out the [good first issue](https://github.com/k8sgpt-ai/k8sgpt/labels/good%20first%20issue) label. +- If you are looking for something to work on, check out our [open issues](https://github.com/k8sgpt-ai/k8sgpt/issues). +- If you have an idea for a new feature, please open an issue, and we can discuss it. +- We are also happy to help you find something to work on. Just reach out to us. + +**Getting in touch with the community** +* Join our [#k8sgpt slack channel](https://slack.cloud-native.io/channels/k8sgpt) +* Introduce yourself on the slack channel or open an issue to let us know that you are interested in contributing + +**Discuss issues** +* Before you start working on something, propose and discuss your solution on the issue +* If you are unsure about something, ask the community + +**How do I contribute?** +- Fork the repository and clone it locally +- Create a new branch and follow [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) guidelines for work undertaken +- Assign yourself to the issue, if you are working on it (if you are not a member of the organization, please leave a comment on the issue) +- Make your changes +- Keep pull requests small and focused, if you have multiple changes, please open multiple PRs +- Create a pull request back to the upstream repository and follow follow the [pull request template](.github/pull_request_template.md) guidelines. +- Wait for a review and address any comments + +**Opening PRs** +- As long as you are working on your PR, please mark it as a draft +- Please make sure that your PR is up-to-date with the latest changes in `main` +- Fill out the PR template +- Mention the issue that your PR is addressing (closes: #) +- Make sure that your PR passes all checks + +**Reviewing PRs** +- Be respectful and constructive +- Assign yourself to the PR +- Check if all checks are passing +- Suggest changes instead of simply commenting on found issues +- If you are unsure about something, ask the author +- If you are not sure if the changes work, try them out +- Reach out to other reviewers if you are unsure about something +- If you are happy with the changes, approve the PR +- Merge the PR once it has all approvals and the checks are passing + +## DCO +We have a DCO check which runs on every PR to verify that the commit has been signed off. + +To sign off the last commit you made, you can use + +``` +git commit --amend --signoff +``` + +You can also automate signing off your commits by adding the following to your `.zshrc` or `.bashrc`: + +``` +git() { + if [ $# -gt 0 ] && [[ "$1" == "commit" ]] ; then + shift + command git commit --signoff "$@" + else + command git "$@" + fi +} +``` + +## Semantic commits +We use [Semantic Commits](https://www.conventionalcommits.org/en/v1.0.0/) to make it easier to understand what a commit does and to build pretty changelogs. Please use the following prefixes for your commits: +- `feat`: A new feature +- `fix`: A bug fix +- `docs`: Documentation changes +- `chores`: Changes to the build process or auxiliary tools and libraries such as documentation generation +- `refactor`: A code change that neither fixes a bug nor adds a feature +- `test`: Adding missing tests or correcting existing tests +- `ci`: Changes to our CI configuration files and scripts + +An example for this could be: +``` +git commit -m "docs: add a new section to the README" +``` + +## Building +Building the binary is as simple as running `go build .` in the root of the repository. If you want to build the container image, you can run `docker build -t k8sgpt -f container/Dockerfile .` in the root of the repository. + +## Releasing +Releases of k8sgpt are done using [Release Please](https://github.com/googleapis/release-please) and [GoReleaser](https://goreleaser.com/). The workflow looks like this: + +* A PR is merged to the `main` branch: + * Release please is triggered, creates or updates a new release PR + * This is done with every merge to main, the current release PR is updated every time + +* Merging the 'release please' PR to `main`: + * Release please is triggered, creates a new release and updates the changelog based on the commit messages + * GoReleaser is triggered, builds the binaries and attaches them to the release + * Containers are created and pushed to the container registry + +> With the next relevant merge, a new release PR will be created and the process starts again + +### Manually setting the version +If you want to manually set the version, you can create a PR with an empty commit message that contains the version number in the commit message. For example: + +Such a commit can get produced as follows: `git commit --allow-empty -m "chore: release 0.0.3" -m "Release-As: 0.0.3` + + diff --git a/README.md b/README.md index 5ffbfaa..3b7174c 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,37 @@ - - Text changing depending on mode. Light: 'So light!' Dark: 'So dark!' + + Text changing depending on mode. Light: 'So light!' Dark: 'So dark!' +_Install it now_ + +``` +brew tap k8sgpt-ai/k8sgpt +brew install k8sgpt +``` + `k8sgpt` is a tool for scanning your kubernetes clusters, diagnosing and triaging issues in simple english. It has SRE experience codified into it's analyzers and helps to pull out the most relevent information to enrich it with AI. - + ## Usage ``` # Ensure KUBECONFIG env is set to an active Kubernetes cluster -k8sgpt auth key - -k8sgpt find problems --explain - +k8sgpt auth +k8sgpt find problems +# for more detail +k8s find problems --explain ``` +## Upcoming major milestones + +- [] Multiple AI backend support +- [] Custom AI/ML model backend support +- [] Custom analyzers + ### What about kubectl-ai? The the kubectl-ai [project](https://github.com/sozercan/kubectl-ai) uses AI to create manifests and apply them to the cluster. It is not what we are trying to do here, it is focusing on writing YAML manifests. @@ -30,5 +43,8 @@ K8sgpt is focused on triaging and diagnosing issues in your cluster. It is a too `k8sgpt` stores config data in `~/.k8sgpt` the data is stored in plain text, including your OpenAI key. +### Contributing + +Please read our [contributing guide](./CONTRIBUTING.md). ### Community * Find us on [Slack](https://cloud-native.slack.com/channels/k8sgpt-ai) diff --git a/cmd/auth/auth.go b/cmd/auth/auth.go index 7289f61..87ff6b6 100644 --- a/cmd/auth/auth.go +++ b/cmd/auth/auth.go @@ -1,29 +1,57 @@ -/* -Copyright © 2023 NAME HERE alexsimonjones@gmail.com -*/ package auth import ( "fmt" + "os" + "strings" + "syscall" + "github.com/fatih/color" "github.com/spf13/cobra" + "github.com/spf13/viper" + "golang.org/x/term" +) + +var ( + backend string ) // authCmd represents the auth command var AuthCmd = &cobra.Command{ Use: "auth", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, + Short: "Authenticate with your chosen backend", + Long: `Provide the necessary credentials to authenticate with your chosen backend.`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("auth called") + + backendType := viper.GetString("backend_type") + if backendType == "" && backend == "" { + color.Red("No backend set. Please run k8sgpt auth") + os.Exit(1) + } + // override the default backend if a flag is provided + if backend != "" { + backendType = backend + } + + fmt.Printf("Enter %s Key: ", backendType) + bytePassword, err := term.ReadPassword(int(syscall.Stdin)) + if err != nil { + color.Red("Error reading %s Key from stdin: %s", backendType, + err.Error()) + os.Exit(1) + } + password := strings.TrimSpace(string(bytePassword)) + + viper.Set(fmt.Sprintf("%s_key", backendType), password) + if err := viper.WriteConfig(); err != nil { + color.Red("Error writing config file: %s", err.Error()) + os.Exit(1) + } + color.Green("key added") }, } func init() { - + // add flag for backend + AuthCmd.Flags().StringVarP(&backend, "backend", "b", "openai", "Backend AI provider") } diff --git a/cmd/auth/key.go b/cmd/auth/key.go deleted file mode 100644 index 0f8b9f4..0000000 --- a/cmd/auth/key.go +++ /dev/null @@ -1,45 +0,0 @@ -/* -Copyright © 2023 NAME HERE alexsimonjones@gmail.com -*/ -package auth - -import ( - "fmt" - "os" - "strings" - "syscall" - - "github.com/fatih/color" - "github.com/spf13/cobra" - "github.com/spf13/viper" - "golang.org/x/term" -) - -// keyCmd represents the key command -var keyCmd = &cobra.Command{ - Use: "key", - Short: "Add a key to OpenAI", - Long: `This command will add a key from OpenAI to enable you to interact with the API`, - Run: func(cmd *cobra.Command, args []string) { - - fmt.Print("Enter OpenAI API Key: ") - bytePassword, err := term.ReadPassword(int(syscall.Stdin)) - if err != nil { - color.Red("Error reading OpenAI API Key from stdin: %s", err.Error()) - os.Exit(1) - } - password := strings.TrimSpace(string(bytePassword)) - - viper.Set("openai_api_key", password) - if err := viper.WriteConfig(); err != nil { - color.Red("Error writing config file: %s", err.Error()) - os.Exit(1) - } - color.Green("key added") - }, -} - -func init() { - AuthCmd.AddCommand(keyCmd) - -} diff --git a/cmd/find/find.go b/cmd/find/find.go index ba8473f..7c5387c 100644 --- a/cmd/find/find.go +++ b/cmd/find/find.go @@ -1,6 +1,3 @@ -/* -Copyright © 2023 NAME HERE alexsimonjones@gmail.com -*/ package find import ( diff --git a/cmd/find/problems.go b/cmd/find/problems.go index 37cdc9e..2c6de8e 100644 --- a/cmd/find/problems.go +++ b/cmd/find/problems.go @@ -1,10 +1,8 @@ -/* -Copyright © 2023 NAME HERE -*/ package find import ( "context" + "fmt" "os" "github.com/fatih/color" @@ -15,7 +13,10 @@ import ( "github.com/spf13/viper" ) -var explain bool +var ( + explain bool + backend string +) // problemsCmd represents the problems command var problemsCmd = &cobra.Command{ @@ -25,10 +26,34 @@ var problemsCmd = &cobra.Command{ provide you with a list of issues that need to be resolved`, Run: func(cmd *cobra.Command, args []string) { - // Initialise the openAI client - openAIClient, err := ai.NewClient() - if err != nil { - color.Red("Error: %v", err) + // get backend from file + backendType := viper.GetString("backend_type") + if backendType == "" { + color.Red("No backend set. Please run k8sgpt auth") + os.Exit(1) + } + // override the default backend if a flag is provided + if backend != "" { + backendType = backend + } + // get the token with viper + token := viper.GetString(fmt.Sprintf("%s_key", backendType)) + // check if nil + if token == "" { + color.Red("No %s key set. Please run k8sgpt auth", backendType) + os.Exit(1) + } + + var aiClient ai.IAI + switch backendType { + case "openai": + aiClient = &ai.OpenAIClient{} + if err := aiClient.Configure(token); err != nil { + color.Red("Error: %v", err) + os.Exit(1) + } + default: + color.Red("Backend not supported") os.Exit(1) } @@ -36,7 +61,7 @@ var problemsCmd = &cobra.Command{ // Get kubernetes client from viper client := viper.Get("kubernetesClient").(*kubernetes.Client) - if err := analyzer.RunAnalysis(ctx, client, openAIClient, explain); err != nil { + if err := analyzer.RunAnalysis(ctx, client, aiClient, explain); err != nil { color.Red("Error: %v", err) os.Exit(1) } @@ -46,7 +71,8 @@ var problemsCmd = &cobra.Command{ func init() { problemsCmd.Flags().BoolVarP(&explain, "explain", "e", false, "Explain the problem to me") - + // add flag for backend + problemsCmd.Flags().StringVarP(&backend, "backend", "b", "openai", "Backend AI provider") FindCmd.AddCommand(problemsCmd) } diff --git a/cmd/root.go b/cmd/root.go index b458de1..4bd9df6 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,6 +1,3 @@ -/* -Copyright © 2023 NAME HERE -*/ package cmd import ( @@ -47,7 +44,6 @@ func init() { // will be global for your application. rootCmd.AddCommand(auth.AuthCmd) rootCmd.AddCommand(find.FindCmd) - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.k8sgpt.git.yaml)") rootCmd.PersistentFlags().StringVar(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.") rootCmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.") @@ -62,6 +58,7 @@ func init() { } viper.Set("kubernetesClient", kubernetesClient) + } // initConfig reads in config file and ENV variables if set. diff --git a/go.mod b/go.mod index a89209b..d4fdcd0 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,11 @@ go 1.20 require ( github.com/briandowns/spinner v1.23.0 github.com/fatih/color v1.15.0 - github.com/sashabaranov/go-openai v1.5.6 + github.com/sashabaranov/go-openai v1.5.7 github.com/spf13/cobra v1.6.1 github.com/spf13/viper v1.15.0 golang.org/x/term v0.6.0 + k8s.io/api v0.26.3 k8s.io/apimachinery v0.26.3 k8s.io/client-go v0.26.3 ) @@ -56,7 +57,6 @@ require ( gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect - k8s.io/api v0.26.3 // indirect k8s.io/klog/v2 v2.80.1 // indirect k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect k8s.io/utils v0.0.0-20221107191617-1a15be271d1d // indirect diff --git a/go.sum b/go.sum index aa76fa1..082db82 100644 --- a/go.sum +++ b/go.sum @@ -214,6 +214,8 @@ github.com/sashabaranov/go-openai v1.5.5 h1:VYdzEGVk4zV04ZNqNb1DT8w7JCzWM77h3h6p github.com/sashabaranov/go-openai v1.5.5/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= github.com/sashabaranov/go-openai v1.5.6 h1:i/DI9y1kzlPqKA0KeTYezJJSy01sqpOdUIm2BV7vgtA= github.com/sashabaranov/go-openai v1.5.6/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= +github.com/sashabaranov/go-openai v1.5.7 h1:8DGgRG+P7yWixte5j720y6yiXgY3Hlgcd0gcpHdltfo= +github.com/sashabaranov/go-openai v1.5.7/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk= github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y= github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w= diff --git a/images/banner-black.png b/images/banner-black.png new file mode 100644 index 0000000..95d1b47 Binary files /dev/null and b/images/banner-black.png differ diff --git a/images/banner-white.png b/images/banner-white.png new file mode 100644 index 0000000..a74f2f9 Binary files /dev/null and b/images/banner-white.png differ diff --git a/images/image.png b/images/image.png new file mode 100644 index 0000000..775a5a3 Binary files /dev/null and b/images/image.png differ diff --git a/pkg/ai/ai.go b/pkg/ai/ai.go index 2e44e70..1c42b37 100644 --- a/pkg/ai/ai.go +++ b/pkg/ai/ai.go @@ -2,36 +2,25 @@ package ai import ( "context" - "fmt" + "errors" "github.com/sashabaranov/go-openai" - "github.com/spf13/viper" ) -type Client struct { +type OpenAIClient struct { client *openai.Client } -func (c *Client) GetClient() *openai.Client { - return c.client -} - -func NewClient() (*Client, error) { - - // get the token with viper - token := viper.GetString("openai_api_key") - // check if nil - if token == "" { - return nil, fmt.Errorf("no OpenAI API Key found") - } - +func (c *OpenAIClient) Configure(token string) error { client := openai.NewClient(token) - return &Client{ - client: client, - }, nil + + if client == nil { + return errors.New("error creating OpenAI client") + } + return nil } -func (c *Client) GetCompletion(ctx context.Context, prompt string) (string, error) { +func (c *OpenAIClient) GetCompletion(ctx context.Context, prompt string) (string, error) { // Create a completion request resp, err := c.client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{ Model: openai.GPT3Dot5Turbo, diff --git a/pkg/ai/iai.go b/pkg/ai/iai.go new file mode 100644 index 0000000..8a3a91e --- /dev/null +++ b/pkg/ai/iai.go @@ -0,0 +1,8 @@ +package ai + +import "context" + +type IAI interface { + Configure(token string) error + GetCompletion(ctx context.Context, prompt string) (string, error) +} diff --git a/pkg/analyzer/analyzer.go b/pkg/analyzer/analyzer.go index 37b957c..4e9b4cd 100644 --- a/pkg/analyzer/analyzer.go +++ b/pkg/analyzer/analyzer.go @@ -2,11 +2,12 @@ package analyzer import ( "context" + "github.com/k8sgpt-ai/k8sgpt/pkg/ai" "github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes" ) -func RunAnalysis(ctx context.Context, client *kubernetes.Client, aiClient *ai.Client, explain bool) error { +func RunAnalysis(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool) error { err := AnalyzePod(ctx, client, aiClient, explain) if err != nil { return err diff --git a/pkg/analyzer/podAnalyzer.go b/pkg/analyzer/podAnalyzer.go index f562c03..c8e1215 100644 --- a/pkg/analyzer/podAnalyzer.go +++ b/pkg/analyzer/podAnalyzer.go @@ -4,17 +4,18 @@ import ( "context" "encoding/base64" "fmt" + "strings" + "time" + "github.com/briandowns/spinner" "github.com/fatih/color" "github.com/k8sgpt-ai/k8sgpt/pkg/ai" "github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes" "github.com/spf13/viper" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "strings" - "time" ) -func AnalyzePod(ctx context.Context, client *kubernetes.Client, aiClient *ai.Client, explain bool) error { +func AnalyzePod(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool) error { // search all namespaces for pods that are not running list, err := client.GetClient().CoreV1().Pods("").List(ctx, metav1.ListOptions{}) diff --git a/pkg/analyzer/rsAnalyzer.go b/pkg/analyzer/rsAnalyzer.go index 2841da2..a08a6d0 100644 --- a/pkg/analyzer/rsAnalyzer.go +++ b/pkg/analyzer/rsAnalyzer.go @@ -4,17 +4,18 @@ import ( "context" "encoding/base64" "fmt" + "strings" + "time" + "github.com/briandowns/spinner" "github.com/fatih/color" "github.com/k8sgpt-ai/k8sgpt/pkg/ai" "github.com/k8sgpt-ai/k8sgpt/pkg/kubernetes" "github.com/spf13/viper" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "strings" - "time" ) -func AnalyzeReplicaSet(ctx context.Context, client *kubernetes.Client, aiClient *ai.Client, explain bool) error { +func AnalyzeReplicaSet(ctx context.Context, client *kubernetes.Client, aiClient ai.IAI, explain bool) error { // search all namespaces for pods that are not running list, err := client.GetClient().AppsV1().ReplicaSets("").List(ctx, metav1.ListOptions{})