Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

feat: generate endpoints using replica numbers#45862

Merged
abeatrix merged 34 commits into
mainfrom
bee/env
Jan 25, 2023
Merged

feat: generate endpoints using replica numbers#45862
abeatrix merged 34 commits into
mainfrom
bee/env

Conversation

@abeatrix

@abeatrix abeatrix commented Dec 20, 2022

Copy link
Copy Markdown
Contributor

RE: sourcegraph/deploy-sourcegraph#4213 & https://github.com/sourcegraph/sourcegraph/issues/45861

Summary

This feature is also backward-compatible, so existing setups and instances will not be affected by this change.

This PR adds the ability to accept replicas number for Gitserver, Indexed-search, Searcher, and Symbols via existing env vars (SEARCHER_URL, SYMBOLS_URL, INDEXED_SEARCH_SERVERS, SRC_GIT_SERVERS), and then generate a new list of endpoints based on replicas number instead of listing out each replica endpoint manually.

Background

Issue: Our current default setup only works in RBAC-enabled clusters.

RBAC for frontend is used for service discovery as it is essential for frontend to arrange communications between services; however, this default setup often leads to delays and frustrating deployment experiences for those without the ability to create RBAC resources. As this is a common deployment pain point and issue that has negative impacts on both the customers and our business, our team believes this is something we should address.

Proposed Solution

A new base cluster introduces in sourcegraph/deploy-sourcegraph#4215 provide an option for users to set up a working Sourcegraph k8s instance with or without RBAC enabled for frontend.

When RBAC resources are removed from frontend, Sourcegraph will not work properly unless a list of services endpoints is included in the frontend env var list using existing env vars that are also being used in our docker-compose deployment , which is a task that relies on users inputting the correct values manually.

The functions introduce in this PR aim to minimize the time our users/customers have to spend configuring their deployment (e.g. spending time inputting the endpoint addresses for each service manually) by generating the endpoints in frontend based on the replica number provided via env vars.

IMPORTANT The changes made in this PR should NOT break any existing setup or instances. No additional changes are required in existing manifests.

Expected Behavior

The current behavior is the same for all instances (both existing instances and new instances), which means:

  • ALL instances that do not have the existing endpoints URLs env vars set are default to using Kubernetes service discovery
  • Setting env vars to empty string "" will return ""
  • See the Examples section listed below for the expected behaviors

Examples:

Return endpoints generated with the replica number:

        env:
        - name: SRC_GIT_SERVERS
          value: "4"
        - name: SEARCHER_URL
          value: "4"
        - name: SYMBOLS_URL
          value: "4"
        - name: INDEXED_SEARCH_SERVERS
          value: "4"

Test plan

  1. copy and paste the context below to your sg.config.overwrite.yaml file in the root of this directory:
  2. run sg start enterprise
  3. You should see frontend and gitserver complaining about the new endpoints that do not exist, with the newly generated list of endpoints in the correct format for k8s deployments.

The expected new endpoints using the file shared below should look like:

  1. 4 gitserver endpoints: "gitserver-0.gitserver:3178", "gitserver-1.gitserver:3178", "gitserver-2.gitserver:3178", "gitserver-3.gitserver:3178", "gitserver-4.gitserver:3178"
  2. 3 indexed search endpoints: "indexed-search-0.indexed-search:6070", "indexed-search-1.indexed-search:6070", "indexed-search-2.indexed-search:6070"
  3. 3 symbols endpoints: "http://symbols-0.symbols:3184", "http://symbols-1.symbols:3184", "http://symbols-2.symbols:3184"
  4. 3 searcher endpoints: "http://searcher-0.searcher:3181", "http://searcher-1.searcher:3181", "http://searcher-2.searcher:3181"

sg.config.overwrite.yaml:

env:
  SRC_GIT_SERVERS: "5"
  SEARCHER_URL: "3"
  SYMBOLS_URL:"3"
  INDEXED_SEARCH_SERVERS: "3"
  DEPLOY_TYPE: kubernetes

Example output from running sg start:

[    gitserver-1] WARN server server/cleanup.go:193 current shard is not included in the list of known gitserver shards, will not delete repos {"current-hostname": "127.0.0.1:3502", "all-shards": ["gitserver-0.gitserver:3178", "gitserver-1.gitserver:3178", "gitserver-2.gitserver:3178", "gitserver-3.gitserver:3178", "gitserver-4.gitserver:3178"]}
[    gitserver-0] WARN server server/cleanup.go:193 current shard is not included in the list of known gitserver shards, will not delete repos {"current-hostname": "127.0.0.1:3501", "all-shards": ["gitserver-0.gitserver:3178", "gitserver-1.gitserver:3178", "gitserver-2.gitserver:3178", "gitserver-3.gitserver:3178", "gitserver-4.gitserver:3178"]}
[       frontend] ERROR InternalHandler httpapi/httpapi.go:281 API HTTP handler error response {"method": "POST", "request_uri": "/.internal/repos/index", "status_code": 500, "error": "hostname \"localhost:3070\" not found in Endpoints{\"indexed-search-0.indexed-search:6070\" \"indexed-search-1.indexed-search:6070\" \"indexed-search-2.indexed-search:6070\"}"}
[       frontend] ERROR internal.http trace/httptrace.go:250 unexpected status code 500 {"route_name": "internal.repos.index", "method": "POST", "url": "/.internal/repos/index", "code": 500, "duration": "2.073375ms", "shouldTrace": false, "error": "hostname \"localhost:3070\" not found in Endpoints{\"indexed-search-0.indexed-search:6070\" \"indexed-search-1.indexed-search:6070\" \"indexed-search-2.indexed-search:6070\"}"}

@cla-bot cla-bot Bot added the cla-signed label Dec 20, 2022
@abeatrix abeatrix requested review from a team and beyang December 20, 2022 20:47
@sourcegraph-bot

sourcegraph-bot commented Dec 20, 2022

Copy link
Copy Markdown
Contributor

Codenotify: Notifying subscribers in CODENOTIFY files for diff 11ba68c...02dd250.

Notify File(s)
@camdencheek internal/search/backend/indexers_test.go
@keegancsmith cmd/symbols/build-ctags.sh
cmd/symbols/internal/api/handler_test.go
internal/search/backend/indexers_test.go
internal/symbols/client.go
internal/symbols/client_test.go
@sourcegraph/delivery doc/admin/deploy/docker-compose/configuration.md

Comment thread CHANGELOG.md Outdated
Comment thread cmd/frontend/internal/cli/config.go Outdated
Comment thread internal/conf/conftypes/conftypes.go Outdated
Comment thread cmd/symbols/build-ctags.sh
Comment thread internal/symbols/client.go Outdated
Comment thread internal/symbols/client.go Outdated
@abeatrix abeatrix requested review from beyang and davejrt January 3, 2023 20:13
Comment thread cmd/frontend/internal/cli/config.go Outdated

@davejrt davejrt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good. It be worth looking into the possibility of using existing envvars that are present in kuberetes or compose to determine the deployment type, rather than having to explicitly define it. For example checking for the presence of one of these could safely determine that it's k8s or not.

/ $ env | grep KUBE
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.253.0.1:443
KUBERNETES_PORT_443_TCP_ADDR=10.253.0.1
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP=tcp://10.253.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_HOST=10.253.0.1

@abeatrix

abeatrix commented Jan 18, 2023

Copy link
Copy Markdown
Contributor Author

Thanks for clarifying @rafax ! I’ve talked to Michael last week regarding this change and he helped me understand why service discovery is important in some use case (e.g standard k8s practice and cli automation etc).

The idea to remove rbac resources from frontend by default is driven by the incidents where customers not having the ability to create them, led to long turnaround time.

We have built a kustomize component that would update the env vars in frontend automatically when deploying using kustomize; however, if service discovery is preferred, the site admins / cluster admins do have the option to add service discovery to frontend anytime (using kustomize).

This allows site admins to move forward with configuring their instance after initial deployment while cluster admins help with deploying RBAC resources collectively--do you think this helps addressing your team's use case here?

@rafax

rafax commented Jan 19, 2023

Copy link
Copy Markdown
Contributor

We have built a kustomize component that would update the env vars in frontend automatically when deploying using kustomize; however, if service discovery is preferred, the site admins / cluster admins do have the option to add service discovery to frontend anytime (using kustomize).

If we have an option to support service discovery (and therefore, can scale deployments without reconfiguring clients / redeploying kustomize) we're good on Cloud side.

@abeatrix abeatrix requested a review from keegancsmith January 24, 2023 16:43
@keegancsmith

Copy link
Copy Markdown
Member

@abeatrix I'll take a look tomorrow, but I noticed that there are a lot of conflicts. You probably wanna merge in origin/main or rebase onto it.

@abeatrix abeatrix requested a review from emidoots January 24, 2023 18:46
@abeatrix

Copy link
Copy Markdown
Contributor Author

adding @slimsag as a reviewer to make sure the new changes will also work with App as expected

Comment thread CHANGELOG.md Outdated
Comment thread cmd/frontend/internal/cli/config.go Outdated
Comment thread cmd/frontend/internal/cli/config.go Outdated
Comment thread cmd/frontend/internal/cli/config.go Outdated
Comment thread cmd/frontend/internal/cli/config.go Outdated
Comment thread cmd/frontend/internal/cli/config.go
Comment thread internal/conf/deploy/deploytype.go Outdated

@emidoots emidoots left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM aside from my comments

@abeatrix abeatrix requested a review from beyang January 25, 2023 00:07
@abeatrix abeatrix mentioned this pull request Jan 25, 2023
18 tasks

@keegancsmith keegancsmith left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice code!

One blocking comment inline, so requesting changes.

Comment thread CHANGELOG.md Outdated
Comment thread cmd/frontend/internal/cli/config.go Outdated
Comment thread cmd/frontend/internal/cli/config.go
Comment on lines +520 to +524
// Detect 'go test' and setup default addresses in that case.
p, err := os.Executable()
if err == nil && strings.HasSuffix(p, ".test") {
return "gitserver:3178", nil
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am assuming this messes with your tests? You can't actually validate the default, but this fine we can read code instead :)

Comment thread cmd/frontend/internal/cli/config.go Outdated
Comment thread cmd/frontend/internal/cli/config.go Outdated
Comment thread cmd/frontend/internal/cli/config_test.go Outdated
Comment thread cmd/frontend/internal/cli/config.go
Comment thread internal/symbols/client.go Outdated
return conns.Symbols
})
})
return symbolsURLs.String()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocker: This change is not safe. It will use a static configuration that won't change over the lifetime of the process. Additionally I worry about potential deadlocks if the conf package is not ready given this is called inside of LoadConfig.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This required a bit more context to fix, so I went ahead and pushed a fix for this :)

Comment thread internal/symbols/client.go Outdated
return conns.Symbols
})
})
return symbolsURLs.String()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This required a bit more context to fix, so I went ahead and pushed a fix for this :)

abeatrix and others added 3 commits January 25, 2023 07:21
@abeatrix abeatrix merged commit c03722f into main Jan 25, 2023
@abeatrix abeatrix deleted the bee/env branch January 25, 2023 19:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants