// handleMaster performs one loop of master locking. // on success it returns <master>, nil // on error it returns "", err // in situations where you should try again due to concurrent state changes (e.g. another actor simultaneously acquiring the lock) // it returns "", nil func (e *etcdMasterElector) handleMaster(path, id string, ttl uint64) (string, error) { res, err := e.etcd.Get(path, false, false) // Unexpected error, bail out if err != nil && !etcdstorage.IsEtcdNotFound(err) { return "", err } // There is no master, try to become the master. if err != nil && etcdstorage.IsEtcdNotFound(err) { return e.becomeMaster(path, id, ttl) } // This should never happen. if res.Node == nil { return "", fmt.Errorf("unexpected response: %#v", res) } // We're not the master, just return the current value if res.Node.Value != id { return res.Node.Value, nil } // We are the master, try to extend out lease return e.extendMaster(path, id, ttl, res) }
func testInstallThirdPartyAPIDeleteVersion(t *testing.T, version string) { _, etcdserver, server, assert := initThirdParty(t, version) defer server.Close() defer etcdserver.Terminate(t) client := etcdserver.Client expectedObj := Foo{ ObjectMeta: api.ObjectMeta{ Name: "test", Namespace: "default", }, TypeMeta: unversioned.TypeMeta{ Kind: "Foo", }, SomeField: "test field", OtherField: 10, } if !assert.NoError(storeToEtcd(client, "/ThirdPartyResourceData/company.com/foos/default/test", "test", expectedObj)) { t.FailNow() return } resp, err := http.Get(server.URL + "/apis/company.com/" + version + "/namespaces/default/foos/test") if !assert.NoError(err) { return } assert.Equal(http.StatusOK, resp.StatusCode) item := Foo{} assert.NoError(decodeResponse(resp, &item)) // Fill in fields set by the apiserver expectedObj.SelfLink = item.SelfLink expectedObj.ResourceVersion = item.ResourceVersion expectedObj.Namespace = item.Namespace if !assert.True(reflect.DeepEqual(item, expectedObj)) { t.Errorf("expected:\n%v\nsaw:\n%v\n", expectedObj, item) } resp, err = httpDelete(server.URL + "/apis/company.com/" + version + "/namespaces/default/foos/test") if !assert.NoError(err) { return } assert.Equal(http.StatusOK, resp.StatusCode) resp, err = http.Get(server.URL + "/apis/company.com/" + version + "/namespaces/default/foos/test") if !assert.NoError(err) { return } assert.Equal(http.StatusNotFound, resp.StatusCode) expectedDeletedKey := etcdtest.AddPrefix("ThirdPartyResourceData/company.com/foos/default/test") _, err = client.Get(expectedDeletedKey, false, false) if !etcdstorage.IsEtcdNotFound(err) { t.Errorf("expected deletion didn't happen: %v", err) } }
// acquireOrRenewLease either races to acquire a new master lease, or update the existing master's lease // returns true if we have the lease, and an error if one occurs. // TODO: use the master election utility once it is merged in. func (c *config) acquireOrRenewLease(etcdClient *etcd.Client) (bool, error) { result, err := etcdClient.Get(c.key, false, false) if err != nil { if etcdstorage.IsEtcdNotFound(err) { // there is no current master, try to become master, create will fail if the key already exists _, err := etcdClient.Create(c.key, c.whoami, c.ttl) if err != nil { return false, err } c.lastLease = time.Now() return true, nil } return false, err } if result.Node.Value == c.whoami { glog.Infof("key already exists, we are the master (%s)", result.Node.Value) // we extend our lease @ 1/2 of the existing TTL, this ensures the master doesn't flap around if result.Node.Expiration.Sub(time.Now()) < time.Duration(c.ttl/2)*time.Second { _, err := etcdClient.CompareAndSwap(c.key, c.whoami, c.ttl, c.whoami, result.Node.ModifiedIndex) if err != nil { return false, err } } c.lastLease = time.Now() return true, nil } glog.Infof("key already exists, the master is %s, sleeping.", result.Node.Value) return false, nil }
// GetAndTestEtcdClient creates an etcd client based on the provided config. It will attempt to // connect to the etcd server and block until the server responds at least once, or return an // error if the server never responded. func GetAndTestEtcdClient(etcdClientInfo configapi.EtcdConnectionInfo) (*etcdclient.Client, error) { // etcd does a poor job of setting up the transport - use the Kube client stack transport, err := client.TransportFor(&client.Config{ TLSClientConfig: client.TLSClientConfig{ CertFile: etcdClientInfo.ClientCert.CertFile, KeyFile: etcdClientInfo.ClientCert.KeyFile, CAFile: etcdClientInfo.CA, }, WrapTransport: DefaultEtcdClientTransport, }) if err != nil { return nil, err } etcdClient := etcdclient.NewClient(etcdClientInfo.URLs) etcdClient.SetTransport(transport.(*http.Transport)) for i := 0; ; i++ { _, err := etcdClient.Get("/", false, false) if err == nil || etcdstorage.IsEtcdNotFound(err) { break } if i > 100 { return nil, fmt.Errorf("could not reach etcd: %v", err) } time.Sleep(50 * time.Millisecond) } return etcdClient, nil }
// InterpretDeleteError converts a generic etcd error on a delete // operation into the appropriate API error. func InterpretDeleteError(err error, kind, name string) error { switch { case etcdstorage.IsEtcdNotFound(err): return errors.NewNotFound(kind, name) default: return err } }
// InterpretDeleteError converts a generic etcd error on a delete // operation into the appropriate API error. func InterpretDeleteError(err error, kind, name string) error { switch { case etcdstorage.IsEtcdNotFound(err): return errors.NewNotFound(kind, name) case etcdstorage.IsEtcdUnreachable(err): return errors.NewServerTimeout(kind, "delete", 2) // TODO: make configurable or handled at a higher level default: return err } }
func (s *SchedulerServer) fetchFrameworkID(client tools.EtcdClient) (*mesos.FrameworkID, error) { if s.FailoverTimeout > 0 { if response, err := client.Get(meta.FrameworkIDKey, false, false); err != nil { if !etcdstorage.IsEtcdNotFound(err) { return nil, fmt.Errorf("unexpected failure attempting to load framework ID from etcd: %v", err) } log.V(1).Infof("did not find framework ID in etcd") } else if response.Node.Value != "" { log.Infof("configuring FrameworkInfo with Id found in etcd: '%s'", response.Node.Value) return mutil.NewFrameworkID(response.Node.Value), nil } } else { //TODO(jdef) this seems like a totally hackish way to clean up the framework ID if _, err := client.Delete(meta.FrameworkIDKey, true); err != nil { if !etcdstorage.IsEtcdNotFound(err) { return nil, fmt.Errorf("failed to delete framework ID from etcd: %v", err) } log.V(1).Infof("nothing to delete: did not find framework ID in etcd") } } return nil, nil }
// TestEtcdClient verifies a client is functional. It will attempt to // connect to the etcd server and block until the server responds at least once, or return an // error if the server never responded. func TestEtcdClient(etcdClient *etcdclient.Client) error { for i := 0; ; i++ { _, err := etcdClient.Get("/", false, false) if err == nil || etcdstorage.IsEtcdNotFound(err) { break } if i > 100 { return fmt.Errorf("could not reach etcd: %v", err) } time.Sleep(50 * time.Millisecond) } return nil }
// Release tries to delete the leader lock. func (e *Etcd) Release() { for i := 0; i < e.maxRetries; i++ { _, err := e.client.CompareAndDelete(e.key, e.value, 0) if err == nil { break } // If the value has changed, we don't hold the lease. If the key is missing we don't // hold the lease. if storage.IsEtcdTestFailed(err) || storage.IsEtcdNotFound(err) { break } util.HandleError(fmt.Errorf("unable to release %s: %v", e.key, err)) } }
// Refresh reloads the RangeAllocation from etcd. func (e *Etcd) Refresh() (*api.RangeAllocation, error) { e.lock.Lock() defer e.lock.Unlock() existing := &api.RangeAllocation{} if err := e.storage.Get(e.baseKey, existing, false); err != nil { if etcdstorage.IsEtcdNotFound(err) { return nil, nil } return nil, etcderr.InterpretGetError(err, e.kind, "") } return existing, nil }
// acquireOrRenewLease either races to acquire a new master lease, or update the existing master's lease // returns true if we have the lease, and an error if one occurs. // TODO: use the master election utility once it is merged in. func (c *config) acquireOrRenewLease(etcdClient *etcd.Client) (bool, error) { keysAPI := etcd.NewKeysAPI(*etcdClient) resp, err := keysAPI.Get(context.TODO(), c.key, nil) if err != nil { if etcdstorage.IsEtcdNotFound(err) { // there is no current master, try to become master, create will fail if the key already exists opts := etcd.SetOptions{ TTL: time.Duration(c.ttl) * time.Second, PrevExist: "", } _, err := keysAPI.Set(context.TODO(), c.key, c.whoami, &opts) if err != nil { return false, err } c.lastLease = time.Now() return true, nil } return false, err } if resp.Node.Value == c.whoami { glog.Infof("key already exists, we are the master (%s)", resp.Node.Value) // we extend our lease @ 1/2 of the existing TTL, this ensures the master doesn't flap around if resp.Node.Expiration.Sub(time.Now()) < time.Duration(c.ttl/2)*time.Second { opts := etcd.SetOptions{ TTL: time.Duration(c.ttl) * time.Second, PrevValue: c.whoami, PrevIndex: resp.Node.ModifiedIndex, } _, err := keysAPI.Set(context.TODO(), c.key, c.whoami, &opts) if err != nil { return false, err } } c.lastLease = time.Now() return true, nil } glog.Infof("key already exists, the master is %s, sleeping.", resp.Node.Value) return false, nil }
func (cfg *Config) Client(check bool) (*etcd.Client, error) { cfg.bindEnv() client, err := etcdClient(cfg) if err != nil { return nil, err } if check { for i := 0; ; i += 1 { _, err := client.Get("/", false, false) if err == nil || etcdstorage.IsEtcdNotFound(err) { break } if i > 100 { return nil, fmt.Errorf("Could not reach etcd at %q: %v", cfg.EtcdAddr.URL, err) } time.Sleep(50 * time.Millisecond) } } return client, nil }
// tryHold attempts to hold on to the lease by repeatedly refreshing its TTL. // If the lease hold fails, is deleted, or changed to another user. The provided // index is used to watch from. // TODO: currently if we miss the watch window, we will error and try to recreate // the lock. It's likely we will lose the lease due to that. func (e *Etcd) tryHold(ttl, index uint64) error { // watch for termination stop := make(chan struct{}) lost := make(chan struct{}) watchIndex := index go util.Until(func() { index, err := e.waitForExpiration(true, watchIndex, stop) watchIndex = index if err != nil { util.HandleError(fmt.Errorf("error watching for lease expiration %s: %v", e.key, err)) return } glog.V(4).Infof("Lease %s lost due to deletion", e.key) close(lost) }, 100*time.Millisecond, stop) defer close(stop) duration := time.Duration(ttl) * time.Second after := time.Duration(float32(duration) * e.waitFraction) last := duration - after interval := last / time.Duration(e.maxRetries) if interval < e.minimumRetryInterval { interval = e.minimumRetryInterval } // as long as we can renew the lease, loop for { select { case <-time.After(after): err := wait.Poll(interval, last, func() (bool, error) { glog.V(4).Infof("Renewing lease %s", e.key) resp, err := e.client.CompareAndSwap(e.key, e.value, e.ttl, e.value, index-1) switch { case err == nil: index = eventIndexFor(resp) return true, nil case storage.IsEtcdTestFailed(err): return false, fmt.Errorf("another client has taken the lease %s: %v", e.key, err) case storage.IsEtcdNotFound(err): return false, fmt.Errorf("another client has revoked the lease %s", e.key) default: util.HandleError(fmt.Errorf("unexpected error renewing lease %s: %v", e.key, err)) index = etcdIndexFor(err, index) // try again return false, nil } }) switch err { case nil: // wait again glog.V(4).Infof("Lease %s renewed", e.key) case wait.ErrWaitTimeout: return fmt.Errorf("unable to renew lease %s: %v", e.key, err) default: return fmt.Errorf("lost lease %s: %v", e.key, err) } case <-lost: return fmt.Errorf("the lease has been lost %s", e.key) } } }
func testInstallThirdPartyResourceRemove(t *testing.T, version string) { master, etcdserver, server, assert := initThirdParty(t, version) defer server.Close() defer etcdserver.Terminate(t) expectedObj := Foo{ ObjectMeta: api.ObjectMeta{ Name: "test", }, TypeMeta: unversioned.TypeMeta{ Kind: "Foo", }, SomeField: "test field", OtherField: 10, } if !assert.NoError(storeThirdPartyObject(master.thirdPartyStorage, "/ThirdPartyResourceData/company.com/foos/default/test", "test", expectedObj)) { t.FailNow() return } secondObj := expectedObj secondObj.Name = "bar" if !assert.NoError(storeThirdPartyObject(master.thirdPartyStorage, "/ThirdPartyResourceData/company.com/foos/default/bar", "bar", secondObj)) { t.FailNow() return } resp, err := http.Get(server.URL + "/apis/company.com/" + version + "/namespaces/default/foos/test") if !assert.NoError(err) { t.FailNow() return } if resp.StatusCode != http.StatusOK { t.Errorf("unexpected status: %v", resp) } item := Foo{} if err := decodeResponse(resp, &item); err != nil { t.Errorf("unexpected error: %v", err) } // TODO: validate etcd set things here item.ObjectMeta = expectedObj.ObjectMeta if !assert.True(reflect.DeepEqual(item, expectedObj)) { t.Errorf("expected:\n%v\nsaw:\n%v\n", expectedObj, item) } path := makeThirdPartyPath("company.com") master.RemoveThirdPartyResource(path) resp, err = http.Get(server.URL + "/apis/company.com/" + version + "/namespaces/default/foos/test") if !assert.NoError(err) { return } if resp.StatusCode != http.StatusNotFound { t.Errorf("unexpected status: %v", resp) } expectedDeletedKeys := []string{ etcdtest.AddPrefix("/ThirdPartyResourceData/company.com/foos/default/test"), etcdtest.AddPrefix("/ThirdPartyResourceData/company.com/foos/default/bar"), } for _, key := range expectedDeletedKeys { thirdPartyObj := extensions.ThirdPartyResourceData{} err := master.thirdPartyStorage.Get(context.TODO(), key, &thirdPartyObj, false) if !etcdstorage.IsEtcdNotFound(err) { t.Errorf("expected deletion didn't happen: %v", err) } } installed := master.ListThirdPartyResources() if len(installed) != 0 { t.Errorf("Resource(s) still installed: %v", installed) } services := master.handlerContainer.RegisteredWebServices() for ix := range services { if strings.HasPrefix(services[ix].RootPath(), "/apis/company.com") { t.Errorf("Web service still installed at %s: %#v", services[ix].RootPath(), services[ix]) } } }
func TestUserInitialization(t *testing.T) { masterConfig, clusterAdminKubeConfig, err := testutil.StartTestMaster() if err != nil { t.Fatalf("unexpected error: %v", err) } clusterAdminClient, err := testutil.GetClusterAdminClient(clusterAdminKubeConfig) if err != nil { t.Fatalf("unexpected error: %v", err) } etcdClient, err := etcd.GetAndTestEtcdClient(masterConfig.EtcdClientInfo) if err != nil { t.Errorf("unexpected error: %v", err) } etcdHelper, err := origin.NewEtcdStorage(etcdClient, masterConfig.EtcdStorageConfig.OpenShiftStorageVersion, masterConfig.EtcdStorageConfig.OpenShiftStoragePrefix) if err != nil { t.Errorf("unexpected error: %v", err) } userRegistry := userregistry.NewRegistry(useretcd.NewREST(etcdHelper)) identityRegistry := identityregistry.NewRegistry(identityetcd.NewREST(etcdHelper)) useridentityMappingRegistry := useridentitymapping.NewRegistry(useridentitymapping.NewREST(userRegistry, identityRegistry)) lookup := identitymapper.NewLookupIdentityMapper(useridentityMappingRegistry, userRegistry) provisioner := identitymapper.NewAlwaysCreateUserIdentityToUserMapper(identityRegistry, userRegistry) testcases := map[string]struct { Identity authapi.UserIdentityInfo Mapper authapi.UserIdentityMapper CreateIdentity *api.Identity CreateUser *api.User CreateMapping *api.UserIdentityMapping UpdateUser *api.User ExpectedErr error ExpectedUserName string ExpectedFullName string }{ "lookup missing identity": { Identity: makeIdentityInfo("idp", "bob", nil), Mapper: lookup, ExpectedErr: kerrs.NewNotFound("UserIdentityMapping", "idp:bob"), }, "lookup existing identity": { Identity: makeIdentityInfo("idp", "bob", nil), Mapper: lookup, CreateUser: makeUser("mappeduser"), CreateIdentity: makeIdentity("idp", "bob"), CreateMapping: makeMapping("mappeduser", "idp:bob"), ExpectedUserName: "******", }, "provision missing identity and user": { Identity: makeIdentityInfo("idp", "bob", nil), Mapper: provisioner, ExpectedUserName: "******", }, "provision missing identity and user with preferred username and display name": { Identity: makeIdentityInfo("idp", "bob", map[string]string{authapi.IdentityDisplayNameKey: "Bob, Sr.", authapi.IdentityPreferredUsernameKey: "admin"}), Mapper: provisioner, ExpectedUserName: "******", ExpectedFullName: "Bob, Sr.", }, "provision missing identity for existing user": { Identity: makeIdentityInfo("idp", "bob", nil), Mapper: provisioner, CreateUser: makeUser("bob", "idp:bob"), ExpectedUserName: "******", }, "provision missing identity with conflicting user": { Identity: makeIdentityInfo("idp", "bob", nil), Mapper: provisioner, CreateUser: makeUser("bob"), ExpectedUserName: "******", }, "provision missing identity with conflicting user and preferred username": { Identity: makeIdentityInfo("idp", "bob", map[string]string{authapi.IdentityPreferredUsernameKey: "admin"}), Mapper: provisioner, CreateUser: makeUser("admin"), ExpectedUserName: "******", }, "provision with existing unmapped identity": { Identity: makeIdentityInfo("idp", "bob", nil), Mapper: provisioner, CreateIdentity: makeIdentity("idp", "bob"), ExpectedErr: kerrs.NewNotFound("UserIdentityMapping", "idp:bob"), }, "provision with existing mapped identity with invalid user UID": { Identity: makeIdentityInfo("idp", "bob", nil), Mapper: provisioner, CreateUser: makeUser("mappeduser"), CreateIdentity: makeIdentityWithUserReference("idp", "bob", "mappeduser", "invalidUID"), ExpectedErr: kerrs.NewNotFound("UserIdentityMapping", "idp:bob"), }, "provision with existing mapped identity without user backreference": { Identity: makeIdentityInfo("idp", "bob", nil), Mapper: provisioner, CreateUser: makeUser("mappeduser"), CreateIdentity: makeIdentity("idp", "bob"), CreateMapping: makeMapping("mappeduser", "idp:bob"), // Update user to a version which does not reference the identity UpdateUser: makeUser("mappeduser"), ExpectedErr: kerrs.NewNotFound("UserIdentityMapping", "idp:bob"), }, "provision returns existing mapping": { Identity: makeIdentityInfo("idp", "bob", nil), Mapper: provisioner, CreateUser: makeUser("mappeduser"), CreateIdentity: makeIdentity("idp", "bob"), CreateMapping: makeMapping("mappeduser", "idp:bob"), ExpectedUserName: "******", }, } for k, testcase := range testcases { // Cleanup if err := etcdHelper.RecursiveDelete(useretcd.EtcdPrefix, true); err != nil && !etcdstorage.IsEtcdNotFound(err) { t.Fatalf("Could not clean up users: %v", err) } if err := etcdHelper.RecursiveDelete(identityetcd.EtcdPrefix, true); err != nil && !etcdstorage.IsEtcdNotFound(err) { t.Fatalf("Could not clean up identities: %v", err) } // Pre-create items if testcase.CreateUser != nil { _, err := clusterAdminClient.Users().Create(testcase.CreateUser) if err != nil { t.Errorf("%s: Could not create user: %v", k, err) continue } } if testcase.CreateIdentity != nil { _, err := clusterAdminClient.Identities().Create(testcase.CreateIdentity) if err != nil { t.Errorf("%s: Could not create identity: %v", k, err) continue } } if testcase.CreateMapping != nil { _, err := clusterAdminClient.UserIdentityMappings().Update(testcase.CreateMapping) if err != nil { t.Errorf("%s: Could not create mapping: %v", k, err) continue } } if testcase.UpdateUser != nil { if testcase.UpdateUser.ResourceVersion == "" { existingUser, err := clusterAdminClient.Users().Get(testcase.UpdateUser.Name) if err != nil { t.Errorf("%s: Could not get user to update: %v", k, err) continue } testcase.UpdateUser.ResourceVersion = existingUser.ResourceVersion } _, err := clusterAdminClient.Users().Update(testcase.UpdateUser) if err != nil { t.Errorf("%s: Could not update user: %v", k, err) continue } } // Spawn 5 simultaneous mappers to test race conditions var wg sync.WaitGroup for i := 0; i < 5; i++ { wg.Add(1) go func() { defer wg.Done() userInfo, err := testcase.Mapper.UserFor(testcase.Identity) if err != nil { if testcase.ExpectedErr == nil { t.Errorf("%s: Expected success, got error '%v'", k, err) } else if err.Error() != testcase.ExpectedErr.Error() { t.Errorf("%s: Expected error %v, got '%v'", k, testcase.ExpectedErr.Error(), err) } return } if err == nil && testcase.ExpectedErr != nil { t.Errorf("%s: Expected error '%v', got none", k, testcase.ExpectedErr) return } if userInfo.GetName() != testcase.ExpectedUserName { t.Errorf("%s: Expected username %s, got %s", k, testcase.ExpectedUserName, userInfo.GetName()) return } user, err := clusterAdminClient.Users().Get(userInfo.GetName()) if err != nil { t.Errorf("%s: Error getting user: %v", k, err) } if user.FullName != testcase.ExpectedFullName { t.Errorf("%s: Expected full name %s, got %s", k, testcase.ExpectedFullName, user.FullName) } }() } wg.Wait() } }